lm function example with R

To illustarte the lm function, we’ll store the year values 2000 to 2015 in x and we will assign values to y which is twice of x plus a randomly generated number.

Then we will store the model in fit and apply it to a serie 0 to 10 to get the corresponding prediction:

Here is the code that you can use in R:

x <- 2000:2015
y <- 2*x+ rnorm(length(x))
fit <- lm(y~x-1)
summary(fit)
newdata <- data.frame(x=0:10)
predict(fit,newdata)

Leave a Reply

Your email address will not be published.