示例
代碼
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)