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?