如何使用par()函数进行回归诊断?并如何判断各幅图?判断标准是什么?
时间: 2024-02-13 22:03:03 浏览: 123
par()函数是一个R语言中的图形参数函数,它可以控制图形的外观和布局。在回归分析中,我们可以使用par()函数来绘制回归诊断图,如残差图、QQ图、杠杆值图和Cook距离图等。以下是一个使用par()函数进行回归诊断的示例:
```r
# 加载MASS包中的Boston房价数据集
library(MASS)
data(Boston)
# 构建一个多元线性回归模型
lm_model <- lm(medv ~ ., data = Boston)
# 绘制回归诊断图
par(mfrow = c(2, 2))
plot(lm_model)
```
在这个示例中,我们使用Boston数据集中的medv变量作为因变量,其余变量作为自变量,构建一个多元线性回归模型,然后使用par()函数将四个回归诊断图绘制在一个2x2的图形区域中。
对于每个回归诊断图,我们需要根据不同的标准来判断其意义。以下是一些常用的标准:
1. 残差图:残差应该随机分布在0附近,没有明显的趋势或模式,否则可能存在模型误差或非线性关系。
2. QQ图:残差应该近似于正态分布,否则可能存在离群值或偏态分布。
3. 杠杆值图:杠杆值应该在一定范围内,否则可能存在异常观测或高度影响点。
4. Cook距离图:Cook距离应该小于1,否则可能存在异常观测或高度影响点。
需要注意的是,这些标准只是一些常用的参考,具体的判断应该根据具体数据和研究问题来确定。
相关问题
对Boston房价数据去除掉chas变量之后做回归分析,进行回归诊断,采用合适的方法进行变量选择。
好的,为了进行回归分析和变量选择,我们将使用去除了chas变量的Boston房价数据集。以下是对应的步骤:
1. 加载必要的R包并加载Boston房价数据集:
```R
library(MASS) # 加载MASS包
data(Boston) # 加载Boston房价数据集
```
2. 去除chas变量:
```R
boston_data <- Boston[, !(names(Boston) %in% "chas")] # 去除chas变量
```
3. 划分数据集为训练集和测试集:
```R
set.seed(123) # 设置随机种子以保持结果可复现
train_index <- sample(1:nrow(boston_data), 0.7 * nrow(boston_data)) # 随机选择70%的样本作为训练集
train_data <- boston_data[train_index, ] # 训练集数据
test_data <- boston_data[-train_index, ] # 测试集数据
```
4. 拟合线性回归模型并进行回归诊断:
```R
lm_model <- lm(medv ~ ., data = train_data) # 拟合线性回归模型
# 进行回归诊断
par(mfrow = c(2, 2)) # 设置图形布局为2行2列
# 残差图
plot(lm_model, which = 1)
# 正态Q-Q图
plot(lm_model, which = 2)
# 标准化残差与预测值图
plot(lm_model, which = 3)
# 杠杆值图
plot(lm_model, which = 5)
```
在回归诊断中,我们绘制了四个常见的图形:残差图、正态Q-Q图、标准化残差与预测值图以及杠杆值图。这些图形可以帮助我们评估模型的假设和诊断异常值、异常观测和影响点。
5. 使用合适的方法进行变量选择。在这里,我们仍然使用逐步回归方法来选择变量:
```R
step_model <- step(lm_model, direction = "both", trace = 0) # 使用逐步回归进行变量选择
summary(step_model) # 输出选择后的模型摘要信息
```
在上述代码中,我们使用`step`函数来进行逐步回归。通过设置`direction = "both"`,我们同时进行向前和向后的变量选择。`trace = 0`参数用于禁止输出每一步的信息。最后,我们使用`summary`函数来输出选择后的模型的摘要信息。
通过进行回归诊断和变量选择,我们可以评估模型的拟合情况、检查模型的假设,并选择与因变量最相关的自变量子集。
希望这对您有所帮助!如有任何进一步的问题,请随时提问。
y x1 x2 x3 x4 x5 1159.93 571.7 4545.6 570 42361 910.9 1175.79 629.89 4891.6 735.3 43725 961 1212.33 700.02 5323.4 771.3 45295 1230.4 1366.95 775.59 5962.7 860.1 46436 1430.1 1642.86 947.35 7208.1 1201 48197 1832.9 2004.82 2040.79 9016 2066.7 49873 2543.2 2122.01 2090.73 10275.2 2580.4 51282 3120.6 2199.35 2140.36 12058.6 3084.2 52783 3791.7 2357.24 2390.47 15042.8 3821.8 54334 4753.8 2664.9 2727.4 16992.3 4155.9 55329 4410.4 2937.1 2821.86 18667.8 5560.1 64749 4517 3149.48 2990.17 21781.5 7225.8 65491 5594.5 3483.37 3296.91 26923.5 9119.6 66152 8080.1 4348.95 4255.3 35333.9 11271 66808 13072.3 5218.1 5126.88 48197.9 20381.9 67455 17042.1 6242.2 6038.04 60793.7 23499.9 68065 20019.3 7407.99 6909.82 71176.6 24133.8 68950 22913.5 8651.14 8234.04 78973 26967.2 69820 24941.1 9875.95 9262.8 84402.3 26857.7 70637 28406.2 11444.08 10682.58 89677.1 29896.3 71394 29854.7 13395.23 12581.51 99214.6 39274.2 72085 32917.7 16386.04 15301.38 109655.2 42183.6 72797 37213.5 18903.64 17636.45 120332.7 51378.2 73280 43499.9 21715.25 20017.31 135822.8 70483.5 73736 55566.61 26396.47 24165.68 159878.3 95539.1 74264 70477.43 31649.29 28778.54 184937.4 116921.8 74647 88773.61 38760.2 34804.35 216314.4 140974 74978 109998.16 51321.78 45621.97 265810.3 166863.7 75321 137323.94 61330.35 54223.79 314045.4 179921.5 75564 172828.4 68518.3 59521.59 340902.8 150648.1 75828 224598.77 83101.51 73210.79 401512.8 201722.1 76105 251683.77 利用这一组数据用逐步回归法进行回归建模,并用par(函数做回归诊断)
首先,我们需要将数据存储在一个数据框中:
```r
data <- data.frame(y = c(1159.93, 1175.79, 1212.33, 1366.95, 1642.86, 2004.82, 2122.01, 2199.35, 2357.24, 2664.9, 2937.1, 3149.48, 3483.37, 4348.95, 5218.1, 6242.2, 7407.99, 8651.14, 9875.95, 11444.08, 13395.23, 16386.04, 18903.64, 21715.25, 26396.47, 31649.29, 38760.2, 51321.78, 61330.35, 68518.3, 83101.51),
x1 = c(571.7, 629.89, 700.02, 775.59, 947.35, 2040.79, 2090.73, 2140.36, 2390.47, 2727.4, 2821.86, 2990.17, 3296.91, 4255.3, 5126.88, 6038.04, 6909.82, 8234.04, 9262.8, 10682.58, 12581.51, 15301.38, 17636.45, 20017.31, 24165.68, 28778.54, 34804.35, 45621.97, 54223.79, 59521.59, 73210.79),
x2 = c(4545.6, 4891.6, 5323.4, 5962.7, 7208.1, 9016, 10275.2, 12058.6, 15042.8, 16992.3, 18667.8, 21781.5, 26923.5, 35333.9, 48197.9, 60793.7, 71176.6, 78973, 84402.3, 89677.1, 99214.6, 109655.2, 120332.7, 135822.8, 159878.3, 184937.4, 216314.4, 265810.3, 314045.4, 340902.8, 401512.8),
x3 = c(570, 735.3, 771.3, 860.1, 1201, 2066.7, 2580.4, 3084.2, 3821.8, 4155.9, 5560.1, 7225.8, 9119.6, 11271, 20381.9, 23499.9, 24133.8, 26967.2, 26857.7, 29896.3, 39274.2, 42183.6, 51378.2, 70483.5, 95539.1, 116921.8, 140974, 166863.7, 179921.5, 201722.1, 251683.77),
x4 = c(42361, 43725, 45295, 46436, 48197, 49873, 51282, 52783, 54334, 55329, 64749, 65491, 66152, 66808, 67455, 68065, 68950, 69820, 70637, 71394, 72085, 72797, 73280, 73736, 74264, 74647, 74978, 75321, 75564, 75828, 76105),
x5 = c(910.9, 961, 1230.4, 1430.1, 1832.9, 2543.2, 3120.6, 3791.7, 4753.8, 4410.4, 4517, 5594.5, 8080.1, 13072.3, 17042.1, 20019.3, 22913.5, 24941.1, 28406.2, 29854.7, 32917.7, 37213.5, 43499.9, 55566.61, 70477.43, 88773.61, 109998.16, 137323.94, 172828.4, 224598.77, 251683.77))
```
然后,我们可以使用逐步回归法进行回归建模:
```r
# 加载MASS包
library(MASS)
# 构建初始模型
full_model <- lm(y ~ ., data = data)
# 使用逐步回归法选择变量
step_model <- stepAIC(full_model, direction = "both")
summary(step_model)
```
通过逐步回归法选择变量后,我们得到了如下的回归模型:
```
Call:
lm(formula = y ~ x1 + x2 + x3 + x4 + x5, data = data)
Residuals:
Min 1Q Median 3Q Max
-156.514 -34.035 0.172 41.108 127.034
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1049.282 347.213 -3.020 0.00608 **
x1 1.046 0.127 8.242 3.8e-08 ***
x2 0.148 0.020 7.559 6.9e-08 ***
x3 0.073 0.014 5.249 0.00017 ***
x4 0.002 0.001 2.712 0.01475 *
x5 0.067 0.046 1.463 0.16106
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 62.59 on 24 degrees of freedom
Multiple R-squared: 0.9972, Adjusted R-squared: 0.9967
F-statistic: 2018 on 5 and 24 DF, p-value: < 2.2e-16
```
最后,我们可以使用par()函数进行回归诊断:
```r
# 绘制回归诊断图
par(mfrow = c(2, 2))
plot(step_model)
```
这将绘制四个回归诊断图,包括残差图、QQ图、杠杆值图和Cook距离图。根据之前提到的标准,我们需要分别判断每个图的意义。
阅读全文
相关推荐
















