楼主: 牛尾巴
4283 59

【独家发布】【kindle】R Data Analysis Cookbook - More Than 80 Recipes to Help You Delive [推广有奖]

51
Lisrelchen 发表于 2016-7-25 22:25:30

Linear Regression using R

  1. Linear Regression using R
  2. 1. Load the caret package:
  3. > library(caret)
  4. 2. Read the data:
  5. > auto <- read.csv("auto-mpg.csv")
  6. 3. Convert the categorical variable cylinders into a factor with appropriate renaming of
  7. the levels:
  8. > auto$cylinders <- factor(auto$cylinders,
  9. levels = c(3,4,5,6,8), labels = c("3cyl", "4cyl", "5cyl",
  10. "6cyl", "8cyl"))
  11. 4. Create partitions:
  12. > set.seed(1000)
  13. > t.idx <- createDataPartition(auto$mpg, p = 0.7,
  14. list = FALSE)
  15. 5. See the names of the variables in the data frame:
  16. > names(auto)
  17. 6. Build the linear regression model:
  18. > mod <- lm(mpg ~ ., data = auto[t.idx, -c(1,8,9)])
  19. 7. View the basic results (your results may differ because of random sampling
  20. differences in creating the partitions):
  21. > mod
  22. 8. View more detailed results:
  23. > summary(mod)
  24. 9. Generate predictions for the test data:
  25. > pred <- predict(mod, auto[-t.idx, -c(1,8,9)])
  26. 10. Compute the RMS error on the test data (your results can differ):
  27. > sqrt(mean((pred - auto[-t.idx, 2])^2))
  28. [1] 4.333631
  29. 11. View diagnostic plots of the model:
  30. > par(mfrow = c(2,2))
  31. > plot(mod)
  32. > par(mfrow = c(1,1))
复制代码

52
Nicolle 学生认证  发表于 2016-7-25 23:48:28

Regression Trees using R

提示: 作者被禁止或删除 内容自动屏蔽

53
Nicolle 学生认证  发表于 2016-7-25 23:54:49
提示: 作者被禁止或删除 内容自动屏蔽

54
ppooxooqq 发表于 2016-7-26 14:45:06
thanks for sharing

55
guokuidai 在职认证  发表于 2016-7-29 09:24:08
下来看看

56
dukeZHYCUFE 发表于 2016-7-29 14:36:55
Thanks

57
mny 发表于 2016-7-31 11:22:59
tttttthx

58
Baby-Zhao 发表于 2016-8-2 13:54:47
thanks

59
dafen 发表于 2016-8-7 08:48:19
收下  谢谢分享

60
nicacc 在职认证  发表于 2016-8-7 11:10:22
thank you

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-26 17:58