lm function example with R

By ganpati | R Programming

Dec 05

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)
newdata <- data.frame(x=0:10)

About the Author

Leave a Comment:

Leave a Comment: