- 阅读权限
- 255
- 威望
- 0 级
- 论坛币
- 50288 个
- 通用积分
- 83.6906
- 学术水平
- 253 点
- 热心指数
- 300 点
- 信用等级
- 208 点
- 经验
- 41518 点
- 帖子
- 3256
- 精华
- 14
- 在线时间
- 766 小时
- 注册时间
- 2006-5-4
- 最后登录
- 2022-11-6
|
Linear Regression using R
|
- Linear Regression using R
- 1. Load the caret package:
- > library(caret)
- 2. Read the data:
- > auto <- read.csv("auto-mpg.csv")
- 3. Convert the categorical variable cylinders into a factor with appropriate renaming of
- the levels:
- > auto$cylinders <- factor(auto$cylinders,
- levels = c(3,4,5,6,8), labels = c("3cyl", "4cyl", "5cyl",
- "6cyl", "8cyl"))
- 4. Create partitions:
- > set.seed(1000)
- > t.idx <- createDataPartition(auto$mpg, p = 0.7,
- list = FALSE)
- 5. See the names of the variables in the data frame:
- > names(auto)
- 6. Build the linear regression model:
- > mod <- lm(mpg ~ ., data = auto[t.idx, -c(1,8,9)])
- 7. View the basic results (your results may differ because of random sampling
- differences in creating the partitions):
- > mod
- 8. View more detailed results:
- > summary(mod)
- 9. Generate predictions for the test data:
- > pred <- predict(mod, auto[-t.idx, -c(1,8,9)])
- 10. Compute the RMS error on the test data (your results can differ):
- > sqrt(mean((pred - auto[-t.idx, 2])^2))
- [1] 4.333631
- 11. View diagnostic plots of the model:
- > par(mfrow = c(2,2))
- > plot(mod)
- > par(mfrow = c(1,1))
复制代码
|
|