R语言对回归模型进行协方差分析
时间:2022-07-23
本文章向大家介绍R语言对回归模型进行协方差分析,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
原文链接:http://tecdat.cn/?p=9529
目录
怎么做测试
本示例使用II型平方和 。参数估计值在R中的计算方式不同,
Data = read.table(textConnection(Input),header=TRUE)
plot(x = Data$Temp, y = Data$Pulse, col = Data$Species, pch = 16, xlab = "Temperature", ylab = "Pulse")legend('bottomright', legend = levels(Data$Species), col = 1:2, cex = 1, pch = 16)
协方差分析
Anova Table (Type II tests) Sum Sq Df F value Pr(>F) Temp 4376.1 1 1388.839 < 2.2e-16 ***Species 598.0 1 189.789 9.907e-14 ***Temp:Species 4.3 1 1.357 0.2542 ### Interaction is not significant, so the slope across groups### is not different. model.2 = lm (Pulse ~ Temp + Species, data = Data)library(car)Anova(model.2, type="II")Anova Table (Type II tests) Sum Sq Df F value Pr(>F) Temp 4376.1 1 1371.4 < 2.2e-16 ***Species 598.0 1 187.4 6.272e-14 ***### The category variable (Species) is significant,### so the intercepts among groups are differentCoefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -7.21091 2.55094 -2.827 0.00858 **Temp 3.60275 0.09729 37.032 < 2e-16 ***Speciesniv -10.06529 0.73526 -13.689 6.27e-14 ***### but the calculated results will be identical.### The slope estimate is the same.### The intercept for species 1 (ex) is (intercept).### The intercept for species 2 (niv) is (intercept) + Speciesniv.### This is determined from the contrast coding of the Species### variable shown below, and the fact that Speciesniv is shown in### coefficient table above. nivex 0niv 1
拟合线的简单图解
plot(x = Data$Temp, y = Data$Pulse, col = Data$Species, pch = 16, xlab = "Temperature", ylab = "Pulse")
模型的p值和R平方
Multiple R-squared: 0.9896, Adjusted R-squared: 0.9888F-statistic: 1331 on 2 and 28 DF, p-value: < 2.2e-16
检查模型的假设
线性模型中残差的直方图。这些残差的分布应近似正态。
残差与预测值的关系图。残差应无偏且均等。
### additional model checking plots with: plot(model.2)### alternative: library(FSA); residPlot(model.2)
具有三类和II型平方和的协方差示例分析
本示例使用II型平方和,并考虑具有三个组的情况。
### --------------------------------------------------------------### Analysis of covariance, hypothetical data### --------------------------------------------------------------Data = read.table(textConnection(Input),header=TRUE)
plot(x = Data$Temp, y = Data$Pulse, col = Data$Species, pch = 16, xlab = "Temperature", ylab = "Pulse")legend('bottomright', legend = levels(Data$Species), col = 1:3, cex = 1, pch = 16)
协方差分析
options(contrasts = c("contr.treatment", "contr.poly")) ### These are the default contrasts in RAnova(model.1, type="II") Sum Sq Df F value Pr(>F) Temp 7026.0 1 2452.4187 <2e-16 ***Species 7835.7 2 1367.5377 <2e-16 ***Temp:Species 5.2 2 0.9126 0.4093 ### Interaction is not significant, so the slope among groups### is not different. Anova(model.2, type="II") Sum Sq Df F value Pr(>F) Temp 7026.0 1 2462.2 < 2.2e-16 ***Species 7835.7 2 1373.0 < 2.2e-16 ***Residuals 125.6 44 ### The category variable (Species) is significant,### so the intercepts among groups are differentsummary(model.2)Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -6.35729 1.90713 -3.333 0.00175 **Temp 3.56961 0.07194 49.621 < 2e-16 ***Speciesfake 19.81429 0.66333 29.871 < 2e-16 ***Speciesniv -10.18571 0.66333 -15.355 < 2e-16 ***### The slope estimate is the Temp coefficient.### The intercept for species 1 (ex) is (intercept).### The intercept for species 2 (fake) is (intercept) + Speciesfake.### The intercept for species 3 (niv) is (intercept) + Speciesniv.### This is determined from the contrast coding of the Species### variable shown below.contrasts(Data$Species) fake nivex 0 0fake 1 0niv 0 1
拟合线的简单图解
组合模型的p值和R平方
Multiple R-squared: 0.9919, Adjusted R-squared: 0.9913F-statistic: 1791 on 3 and 44 DF, p-value: < 2.2e-16
检查模型的假设
hist(residuals(model.2), col="darkgray")
线性模型中残差的直方图。这些残差的分布应近似正态。
plot(fitted(model.2), residuals(model.2))
残差与预测值的关系图。残差应无偏且均等。
### additional model checking plots with: plot(model.2)### alternative: library(FSA); residPlot(model.2)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- CentOS7安装调试Mysql数据库的步骤详解【实例】
- 深入理解Bash中的尖括号(适合初学者)
- linux安装php7的方法详解
- centos7.2搭建nginx的web服务器部署uniapp项目
- 通过 SSH 在远程 Linux 系统上运行命令的方法
- 详解Linux Namespace之User
- Centos7.0安装ceph(JEWEL)及以上版本的实例解析
- 详解Linux内核进程调度函数schedule()的触发和执行时机
- Linux下使用SSH远程执行命令方法收集
- 详解Centos7.2编译安装zabbix3.2(详细步骤)
- Linux下误删messages文件的找回方法
- 自制YUM仓库的步骤讲解
- 解决centos7中tomcat启动与本机访问问题
- centos6.5配置ssh免秘钥登陆执行pssh命令的讲解
- 使用PXE自动安装CentOS7.6的教程详解