View Single Post
  #7  
Old 05-14-2013, 05:06 PM
jlaurentum jlaurentum is offline
Member
 
Join Date: Apr 2013
Location: Venezuela
Posts: 41
Default Re: *ANSWER* questions w linear regression & weight decay

Michael:

Here you go:

Code:
#READ IN THE FILES.
datos1 <- read.table("in.dta")
names(datos1) <- c("X1","X2","Y")
datos2 <- read.table("out.dta")
names(datos2) <- c("X1","X2","Y")
#FOR THE FOLLOWING QUESTIONS, SET UP THE MATRIXES
Z <- with(datos1,
			cbind(rep(1,nrow(datos1)),X1,X2,
						X1^2,X2^2,X1*X2,abs(X1-X2),abs(X1+X2)) )
Z <- as.matrix(Z)
Zout <- with(datos2,
					cbind(rep(1,nrow(datos2)),X1,X2,
						X1^2,X2^2,X1*X2,abs(X1-X2),abs(X1+X2)) )
Zout <- as.matrix(Zout)
#NOW FIT WITH WEIGHT DECAY USING LAMBDA=10^-3
lambda <- 10^(-3)

M <- t(Z)%*%Z + diag(rep(8,1))*lambda
w <- solve(M)%*%t(Z)%*%datos1$Y
Ym <- as.numeric(sign(Z%*%w))
Ein <- mean(datos1$Y!=Ym)
Ym <- as.numeric(sign(Zout%*%w))
Eout <- mean(datos2$Y!=Ym)
Reply With Quote