r - customized ridge regression function -


i working on ridge regression, want make own function. tried following. work individual value of k not array sequence of values.

dt<-longley attach(dt) library(mass)  x<-cbind(x1,x2,x3,x4,x5,x6) x<-as.matrix(x) y<-as.matrix(y)  sx<-scale(x)/sqrt(nrow(x)-1) sy<-scale(y)/sqrt(nrow(y)-1) rxx<-cor(sx) rxy<-cor(sx,sy)  (k in 0:1){ res<-solve(rxx+k*diag(rxx))%*%rxy k=k+0.01 } 

need optimized code too.

poly.kernel <- function(v1, v2=v1, p=1) {        ((as.matrix(v1) %*% t(v2))+1)^p }     kernelridgereg <- function(trainobjects,trainlabels,testobjects,lambda){    x <- trainobjects   y <- trainlabels                         kernel <- poly.kernel(x)    design.mat <- cbind(1, kernel)    <- rbind(0, cbind(0, kernel))    m <- crossprod(design.mat) + lambda*i   #crossprod x times  traspose of x, looks neater in openion    m.inv <- solve(m)   #inverse of m    k <- as.matrix(diag(poly.kernel(cbind(trainobjects,trainlabels))))   #removing diag still gives same mse, output vector of prediction.    labels <- rbind(0,as.matrix(trainlabels))    y.hat <- t(labels) %*% m.inv %*% rbind(0,k)    y.true <- y.test    mse <-mean((y.hat - y.true)^2)     return(list(mse=mse,y.hat=y.hat))  } 

kernel p=1, give ridge regression.

solve built-in r function return singular matrix. may want write own function avoid that.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -