1 Энгийн регресийн загвар

\(h_{\theta}{x}\) = \(\theta_{0}\) + \(\theta_{1}\)\(x\)+\(u\)

1.1 Зардлын функц

\(J(\theta_0,\theta_1)\)= \(\frac{1}{2m}\)\(\sum_{i= 1}^{m} (h_\theta(x^i)-y^i)^2\)

\(minimize(J(\theta_0,\theta_1))\)

1.2 градиент алгоритм

\(\theta_{0}\)\(:\)\(=\)\(\theta_{0}\)\(-\)\(\alpha\)\((h_\theta(x^i)-y^i)^2\)

\(\theta_{1}\)\(:\)\(=\)\(\theta_{1}\)\(-\)\(\alpha\)\((h_\theta(x^i)-y^i)^2\)\((x^i)\)

\(\alpha\)= \(learning\ rate\)

1.3 Шугаман регрессийг үнэлэх

library(wooldridge)
library(ggplot2)
attach(wage1)
res<-lm(wage~educ)
res
## 
## Call:
## lm(formula = wage ~ educ)
## 
## Coefficients:
## (Intercept)         educ  
##     -0.9049       0.5414
ggplot(wage1, aes(educ,wage))+geom_point(color="dodgerblue",shape=1)+geom_smooth(method = "lm",se=FALSE,color="red")+theme_bw()

1.4 Machine learning код

library(wooldridge)
x<-educ
y<-wage
cost <- function(X, y, theta) {
  sum( (X %*% theta - y)^2 ) / (2*length(y))
}

# learning rate болон iteration limit
alpha <- 0.01
num_iters <- 90000

# 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,] -0.9048516
## [2,]  0.5413593

1.5 Дата мэдээлэл дээрх график

plot(x,y, col=rgb(0.2,0.4,0.6,0.4), main='Linear regression by gradient descent')
for (i in c(1,3,6,10,14,seq(20,num_iters,by=10))) {
  abline(coef=theta_history[[i]], col=rgb(0.8,0,0,0.3))
}
abline(coef=theta, col='blue')

1.6 Зардлын функц график

plot(cost_history, type='line', col='blue', lwd=2, main='Cost function', ylab='cost', xlab='Iterations')