O'Reilly logo

Regression Analysis with R by Giuseppe Ciaburro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required