Search code examples
rmachine-learninggradient-descent

linear regression by gradient desent in R


I am very new to machine learning and currently is trying to do a linear regression using R, my code is below:

x <- runif(1000, -5, 5)
y <- runif(1000, -2, 2)
z <- x + y
res <- lm(z ~ x + y)
alpha <- 0.01
num_iters <- 10000
theta <- matrix(c(0,0,0), nrow = 3)
X <- cbind(1, matrix(x), matrix(y))

for (i in 1:num_iters){
    cost <- (X %*% theta - y)
    delta <- t(X) %*% cost / length(y)
    theta <- theta - alpha * delta
}

You can see after gradient for 10000 times, the result theta is different with res which resulted by function lm of R, can somebody advise where I missed?


Solution

  • You just need one small change, the variable that you're trying to model is z not y:

    set.seed(0)
    x <- runif(1000,-5,5)
    y <- runif(1000,-2,2)
    z <- x+y
    res <- lm(z~x+y)
    alpha <- 0.01
    num_iters <- 10000
    theta <- matrix(c(0,0,0), nrow=3)
    X <- cbind(1, matrix(x), matrix(y))
    for (i in 1:num_iters){
      cost <- (X%*%theta-z)  ##What was y is now z
      delta <- t(X)%*%cost/length(y)
      theta <- theta-alpha*delta
    }
    
    theta
    #             [,1]
    #[1,] -1.56669e-16
    #[2,]  1.00000e+00
    #[3,]  1.00000e+00