简体   繁体   English

R中的多元线性回归-梯度下降

[英]Multivariate Linear Regression - Gradient Descent in R

I am learning machine learning. 我正在学习机器学习。 So I do some simple practice with the data I find online. 因此,我对在网上找到的数据做了一些简单的练习。 Right now I try to implement linear regression by gradient descent in R. When I run it, I realize that it does not converge and my cost goes high infinitely. 现在,我尝试通过R中的梯度下降实现线性回归。运行它时,我意识到它没有收敛,并且我的成本无限高。 Although I suspect it is somewhere in the part where I calculate gradient, I am not able to find the problem. 尽管我怀疑它在我计算梯度的部分中的某个位置,但是我找不到问题。 So lets start presenting my data. 因此,让我们开始展示我的数据。

My data set contains 4 column : ROLL ~ UNEM, HGRAD, INC So, the goal is finding relationship between ROLL and others. 我的数据集包含4列: ROLL ~ UNEM, HGRAD, INC因此,目标是找到ROLL与其他对象之间的关系。

  • Let me present my code 让我介绍我的代码

     datavar <- read.csv("dataset.csv") attach(datavar) X <- cbind(rep(1, 29), UNEM,HGRAD,INC) y <- ROLL # function where I calculate my prediction h <- function(X, theta){ return(t(theta) %*% X) } # function where I calculate the cost with current values cost <- function(X, y, theta){ result <- sum((X %*% theta - y)^2 ) / (2*length(y)) return(result) } # here I calculate the gradient, #mathematically speaking I calculate derivetive of cost function at given points gradient <- function(X, y, theta){ m <- nrow(X) sum <- c(0,0,0,0) for (i in 1 : m) { sum <- sum + (h(X[i,], theta) - y[i]) * X[i,] } return(sum) } # The main algorithm gradientDescent <- function(X, y, maxit){ alpha <- 0.005 m <- nrow(X) theta <- c(0,0,0,0) cost_history <- rep(0,maxit) for (i in 1 : maxit) { theta <- theta - alpha*(1/m)*gradient(X, y, theta) cost_history[i] <- cost(X, y, theta) } plot(1:maxit, cost_history, type = 'l') return(theta) } 

I run the code like this 我这样运行代码

 gradientDescent(X, y, 20)

This is the output I get : 这是我得到的输出:

-7.001406e+118  -5.427330e+119  -1.192040e+123  -1.956518e+122

So, can you find where I was wrong. 所以,你能找到我错了吗。 I have already tried different alpha values, didn't make a difference. 我已经尝试过不同的Alpha值,没有什么不同。 By the way, I appreciate any tips or good practice from you, 顺便说一句,我感谢您提供的任何提示或好的做法,

Thanks 谢谢

Well, I think I finally found the answer. 好吧,我想我终于找到了答案。 The problem was that I did not appy any feature scaling. 问题是我没有应用任何功能缩放。 Couse I though it was optional precedure for running the algorithm smoothly. 因为虽然这是平稳运行算法的可选步骤,但请注意。 Now it works as expected. 现在它可以按预期工作了。 You can try to run code with scaled dataset using R's scale() function. 您可以尝试使用R的scale()函数对缩放后的数据集运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM