Carlos Carlos - 1 year ago 192
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:


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

#Calculating pseudomatrix of 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
m <- -(weights[1,1]/weights[2,1])
b <- -(weights[3,1]/weights[2,1])

It doesn't work.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download