回归分析
数据预处理
data <- as.data.frame(scale(data, center = T, scale = T)) # 标准化 data <- as.data.frame(scale(data, center = T, scale = F)) # 中心化
变量相关性检验
library(Hmisc) rcorr(as.matrix(data), type = 'pearson') R <- rcorr(as.matrix(data), type = 'pearson')$r # 相关系数阵 P <- rcorr(as.matrix(data), type = 'pearson')$P # 相关性检验的P值library(psych) corr.test(data,use="complete")library(Hmisc) R <- rcorr(as.matrix(data), type = 'pearson')$r # 相关阵 library(corrplot) corrplot(R, method="color", addCoef.col='grey', tl.col='black') # 热力图
image-20221124123849236 library(Hmisc) R <- rcorr(as.matrix(data), type = 'pearson')$r # 相关阵 library(reshape2) melted_r <- melt(R) # 将相关系数矩阵拉直 library(ggplot2) ggplot(data = melted_r, aes(x=Var1,y=Var2,fill = value)) + # ggplot2 基本图层 geom_raster() + # 绘制热力图 scale_fill_gradient2(low = rgb(10,8,32,max = 255),mid = rgb(165,24,90,max = 255), high = rgb(249,228,209,max = 255),limit = c(0,1), name="Pearson\nCorrelation") + theme( # 调整图表样式 axis.title = element_blank(), # 删除ggplot2的坐标标签 panel.background = element_blank(), # 删除ggplot2的灰色背景 panel.grid.major = element_blank(), # 删除ggplot2的面板网格 panel.border = element_blank(), axis.ticks = element_blank() # 删除ggplot2的轴刻度 ) + geom_text(aes(Var2,Var1,label = round(value,2)), color = '#228B22',size = 2) # 加上相关系数数值
image-20221124124310441
偏相关系数

偏相关系数图
建立模型

查看杠杆值(按照X大小顺序排序)
回归模型检验

image-20221124124627132
模型分析

置信区间和预测区间
预测相关
最后更新于