示例

代码

sessionInfo()  #  查询版本及系统和库等信息

# 工作目录设置
getwd()
path <- "E:/RSpace"
setwd(path)

rm(list=ls()) # 清空内存中的变量

# mtcars # 展示基础安装中的 mtcars 数据集
str(mtcars) # 查看 mtcars 的数据结构

summary(mtcars ) # 查看 mtcars  的摘要统计量

# 检测二变量关系
cor(mtcars ) # 相关系数矩阵

# 添加包
install.packages("car")
library(car)

# 创建散点图矩阵
scatterplotMatrix(mtcars , spread=FALSE, smoother.arg=list(lty=2), main="散点图矩阵")


# 多元线性回归
# 研究州犯罪率跟人口、文盲率、平均收入和结霜期的关系。
fit <- lm(mpg ~ hp + wt + hp:wt, data=mtcars )
fit # 查看模型的结果对象列表

summary(fit) # 展示模拟模型的详细结果

# 添加包
install.packages("effects")
library(effects)

# 用图形展示交互项的的结果
# dev.off() # 关闭图形设备
plot(effect("hp:wt", fit, xlevels=list(wt=c(2.2,3.2,4.2))),multiline=TRUE)

相关文章:

  • 2021-06-23
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2021-11-18
  • 2021-04-03
猜你喜欢
  • 2021-08-14
  • 2021-06-29
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-04-02
相关资源
相似解决方案