7.1 回归和相关分析

7.1 回归和相关分析

7.1.1 问题

你想要做线性回归或相关分析。

7.1.2 方案

要处理的一些样例数据:

## 制造一些数据 X 增加(大的干扰噪声) Z 缓慢增加 构建
## Y,它与 X 变量负相关,与 X*Z 变量正相关
set.seed(955)
xvar <- 1:20 + rnorm(20, sd = 3)
zvar <- 1:20/4 + rnorm(20, sd = 2)
yvar <- -2 * xvar + xvar * zvar/5 + 3 + rnorm(20, sd = 4)

## 通过组合已创建的向量来构建数据框
dat <- data.frame(x = xvar, y = yvar, z = zvar)
## 展示前 6 行
head(dat)
#>        x       y        z
#> 1 -4.252   4.586  1.89877
#> 2  1.702  -4.903 -0.82937
#> 3  4.323  -4.308 -1.31283
#> 4  1.781   0.205 -0.28479
#> 5 11.537 -29.767 -1.27304
#> 6  6.672 -10.146 -0.09459

7.1.2.1 相关系数

## 相关系数 - 默认使用 pearson 方法
cor(dat$x, dat$y)
#> [1] -0.7695

7.1.2.2 相关矩阵(多个变量)

同时可以对多个变量进行两两相关性分析,结果是一个 nxn 的平方矩阵或是数据框。

## 变量之间的相关矩阵
cor(dat)
#>         x         y        z
#> x  1.0000 -0.769538 0.491699
#> y -0.7695  1.000000 0.004172
#> z  0.4917  0.004172 1.000000

## 保留两位小数点
round(cor(dat), 2)
#>       x     y    z
#> x  1.00 -0.77 0.49
#> y -0.77  1.00 0.00
#> z  0.49  0.00 1.00

7.1.2.3 线性回归

线性回归,当 dat$x 是预测变量时,dat$y 为响应变量。这可以使用一个数据框的两列,或者是直接使用数值向量。

## 下面两个命令会显示一样的结果
fit <- lm(y ~ x, data = dat)  # 使用数据框的 x 列和 y 列
fit <- lm(dat$y ~ dat$x)  # 使用 dat$x 和 dat$y 进行拟合
fit
#>
#> Call:
#> lm(formula = dat$y ~ dat$x)
#>
#> Coefficients:
#> (Intercept)        dat$x
#>      -0.228       -1.183

## 这说明预测 y = -0.2278 - 1.1829*x 获取更详细的信息
summary(fit)
#>
#> Call:
#> lm(formula = dat$y ~ dat$x)
#>
#> Residuals:
#>     Min      1Q  Median      3Q     Max
#> -15.892  -2.511   0.287   4.465   9.329
#>
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept)   -0.228      2.632   -0.09     0.93
#> dat$x         -1.183      0.231   -5.11  7.3e-05 ***
#> ---
#> Signif. codes:
#> 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 6.51 on 18 degrees of freedom
#> Multiple R-squared:  0.592,  Adjusted R-squared:  0.57
#> F-statistic: 26.1 on 1 and 18 DF,  p-value: 7.28e-05

7.1.2.4 多个预测变量的线性回归(多元线性回归)

使用 y 作为线性回归的响应变量,x 和 z 作为预测变量。

注意下面的公式没有检测 x 与 z 之间的交互效应。

## 这些都有相同的结果
fit2 <- lm(y ~ x + z, data = dat)  # 使用数据框的 x,y,z 列
fit2 <- lm(dat$y ~ dat$x + dat$z)  # 使用向量
fit2
#>
#> Call:
#> lm(formula = dat$y ~ dat$x + dat$z)
#>
#> Coefficients:
#> (Intercept)        dat$x        dat$z
#>       -1.38        -1.56         1.86

summary(fit2)
#>
#> Call:
#> lm(formula = dat$y ~ dat$x + dat$z)
#>
#> Residuals:
#>    Min     1Q Median     3Q    Max
#>  -7.97  -3.19  -1.21   3.85   7.52
#>
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept)   -1.382      1.988   -0.70   0.4964
#> dat$x         -1.564      0.198   -7.88  4.5e-07 ***
#> dat$z          1.858      0.475    3.91   0.0011 **
#> ---
#> Signif. codes:
#> 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 4.86 on 17 degrees of freedom
#> Multiple R-squared:  0.785,  Adjusted R-squared:  0.76
#> F-statistic: 31.1 on 2 and 17 DF,  p-value: 2.1e-06
7.1.2.4.1 交互效应

如何合适地构建多元线性回归并且检验交互效应非常复杂,这里不作讲述。这里我们仅仅用 x 和 z 变量以及它们之间的交互效应拟合模型。

想要构建 x 与 z 之间的交互效应模型,需要添加 x:z 项。我们也可以使用公式 x*z 来代表 x+z+x:z 。

## 下面两个公式等效
fit3 <- lm(y ~ x * z, data = dat)
fit3 <- lm(y ~ x + z + x:z, data = dat)
fit3
#>
#> Call:
#> lm(formula = y ~ x + z + x:z, data = dat)
#>
#> Coefficients:
#> (Intercept)            x            z          x:z
#>       2.282       -2.131       -0.107        0.208

summary(fit3)
#>
#> Call:
#> lm(formula = y ~ x + z + x:z, data = dat)
#>
#> Residuals:
#>    Min     1Q Median     3Q    Max
#> -5.305 -3.600  0.393  2.138  8.396
#>
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)
#> (Intercept)   2.2820     2.2006    1.04    0.315
#> x            -2.1311     0.2741   -7.78    8e-07 ***
#> z            -0.1068     0.8482   -0.13    0.901
#> x:z           0.2081     0.0787    2.64    0.018 *
#> ---
#> Signif. codes:
#> 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 4.18 on 16 degrees of freedom
#> Multiple R-squared:  0.85,   Adjusted R-squared:  0.822
#> F-statistic: 30.3 on 3 and 16 DF,  p-value: 7.76e-07