2017-02-19 65 views
1

我使用波士頓數據集作爲我的輸入,我試圖建立一個模型來預測MEDV(自有住房的中值爲1000美元),使用RM(每間房屋的平均房間數量)R中梯度下降和線性模型之間的θ值差異

我已經從Digitheads blog中混淆了以下代碼,而不是如你所見。

我的代碼如下:

#library(datasets) 
#data("Boston") 

x <- Boston$rm 
y <- Boston$medv 

# fit a linear model 
res <- lm(y ~ x) 
print(res) 

Call: 
lm(formula = y ~ x) 

Coefficients: 
(Intercept)   x 
    -34.671  9.102 

# plot the data and the model 
plot(x,y, col=rgb(0.2,0.4,0.6,0.4), main='Linear regression') 
abline(res, col='blue') 

enter code here

# squared error cost function 
cost <- function(X, y, theta) { 
    sum((X %*% theta - y)^2)/(2*length(y)) 
} 

# learning rate and iteration limit 
alpha <- 0.01 
num_iters <- 1000 

# keep history 
cost_history <- double(num_iters) 
theta_history <- list(num_iters) 

# initialize coefficients 
theta <- matrix(c(0,0), nrow=2) 

# add a column of 1's for the intercept coefficient 
X <- cbind(1, matrix(x)) 

# gradient descent 
for (i in 1:num_iters) { 
    error <- (X %*% theta - y) 
    delta <- t(X) %*% error/length(y) 
    theta <- theta - alpha * delta 
    cost_history[i] <- cost(X, y, theta) 
    theta_history[[i]] <- theta 
} 

print(theta) 

      [,1] 
[1,] -3.431269 
[2,] 4.191125 

作爲每Digitheads博客,其用於使用所述LM(線性模型)和他從梯度下降匹配值THETA值,而我的不是。這些數字不應該匹配嗎?你可以從劇情中看到theta的各種值,我的最後y截距與打印(θ)值幾行不符?

任何人都可以提出一個關於我哪裏出錯的建議嗎?

Linear Regression Gradient Descent

+1

我認爲這只是需要一段時間來收斂。如果我重新運行模型,但將迭代次數增加到50k或100k,我會得到與ols估計相同的結果 – gfgm

+0

@GabrielFGeislerMesevage是的,通過增加迭代次數,我可以得到與OLS相同的GD值。請把評論作爲回答,我會很高興地接受它作爲正確的答案。非常感謝。 – TheGoat

+0

謝謝。添加評論作爲答案。 – gfgm

回答

1

梯度下降需要一段時間才能收斂。增加迭代次數將使模型收斂到OLS值。例如:

# learning rate and iteration limit 
alpha <- 0.01 
num_iters <- 100000 # Here I increase the number of iterations in your code to 100k. 
# The gd algorithm now takes a minute or so to run on my admittedly 
# middle-of-the-line laptop. 

# keep history 
cost_history <- double(num_iters) 
theta_history <- list(num_iters) 

# initialize coefficients 
theta <- matrix(c(0,0), nrow=2) 

# add a column of 1's for the intercept coefficient 
X <- cbind(1, matrix(x)) 

# gradient descent (now takes a little longer!) 
for (i in 1:num_iters) { 
    error <- (X %*% theta - y) 
    delta <- (t(X) %*% error)/length(y) 
    theta <- theta - alpha * delta 
    cost_history[i] <- cost(X, y, theta) 
    theta_history[[i]] <- theta 
} 

print(theta) 
    [,1] 
[1,] -34.670410 
[2,] 9.102076