Carlos Carlos - 1 month ago 30
R Question

Apply SVD Linear Regression in R

I'm trying apply SVD Linear Regression in a points cloud. My representation of points set is a matrix with two colums, where first column is 'x' and second is 'y'. So, I get this plot:

myplot

How I can apply SVD linear regression to my point set with a function?
I've tried with this:

Regress_Lin<-function(data,label){
#Calculating pseudomatrix of data
data<-cbind(data,rep(1,nrow(data)))
data.svd <- svd(data)
ds <- diag(1/data.svd$d)
u <- data.svd$u
v <- data.svd$v
us <- as.matrix(u)
vs <- as.matrix(v)
#Calculating abline coefficients y=mx+b
weights<-(vs%*%solve(ds)%*%t(u))%*%label
m <- -(weights[1,1]/weights[2,1])
b <- -(weights[3,1]/weights[2,1])
c(m,b)
}


It doesn't work.

Answer

You only need SVD to compute the inverse of inverse of X and then you get the Weights for Linear Regression as follows:

Weights (This is the pseudoinverse of X)

So given that you have your data with the format (x0, x1, x2), first:

Get inverse of X with SVD:

x <- t(data) %*% data
duv <- svd(x)

Once you have the singular value descomposition, x^-1 is computed as inverse

x.inv <- duv$v %*% diag(1 / duv$d) %*% t(duv$u)

Now, compute the pseudoinverse of X as pseudoinverse

x.pseudo.inv <- x.inv %*% t(data)

And now you can get the Weights doing w

w <- x.pseudo.inv %*% labels

Hope it helps.