Carlos - 6 months ago 81

R Question

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:

`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 and then you get the Weights for Linear Regression as follows:

(This is the pseudoinverse of X)

So given that you have your `data`

with the format `(x0, x1, x2)`

, first:

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

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

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

Now, compute the pseudoinverse of X as

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

And now you can get the Weights doing

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

Hope it helps.