Linear regression with SGD

To perform a linear regression analysis with the SGD algorithm, we use the example available in the sgd package:

library(sgd)N <- 10000d <- 10set.seed(42)X <- matrix(rnorm(N*d), ncol=d)theta <- rep(5, d+1)eps <- rnorm(N)y <- cbind(1, X) %*% theta + epsdat <- data.frame(y=y, x=X)sgd.theta <- sgd(y ~ ., data=dat, model="lm")sprintf("Mean squared error: %0.3f", mean((theta -                       as.numeric(sgd.theta$coefficients))^2))plot(sgd.theta, theta, type="mse-param")

Now, let's go through the code to understand how to apply the sgd package to solve a linear regression problem.

To start, we must install the sgd package in the R environment.

Remember that to install a library that is not present in the initial distribution of R, ...

Get Regression Analysis with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.