R的lm函数如何处理因子水平(在C_Cdqrls中?)?

时间:2019-07-09 13:19:37

标签: r linear-regression lm

或者换句话说:在这种情况下使用哪种算法?我猜他们使用判别分析作为描述,例如在第4.4章中。在詹姆斯等。等“ R语言中的统计学习及其应用简介”?

在评论输入后,我还可以按如下方式重述该问题:

  • 魔术的第一部分出现在ans <- .External2(C_modelmatrix, t, data)(在model.matrix.default中)中,模型根据因子水平而变化=> 我认为我理解这部分。
  • 第二部分仍然涉及z <- .Call(C_Cdqrls, x, y, tol, FALSE),我不希望线性回归和判别分析在数学上是相同的。 我想念一些明显的东西吗?同样,我的stats包是二进制文件,我无权访问源代码...

我在此article中找到了非常有用的解释,但在某些时候它仅说明

  

...这种[因素]解构可能是一个复杂的任务,因此我们不会   进入细节,以免我们走得太远...

我在文档中找不到任何内容,也无法理解使用debug(lm)的情况 使用可重现的示例我所了解的是:

n <- 10
p <- 6
set.seed(1)
x <- seq(0, 20, length.out = n) + rnorm(n, 0, 1)
y <- c(1:3)
y <- sample(y, n, replace = TRUE)
z <- 10*y*x + 10*y + 10 + rnorm(n, 0, 1)
debug(lm)
fit <- lm(z ~ x*y)

mt <- attr(mf, "terms")之后看起来像

mt
# ...
# attr(,"dataClasses")
#         z         x         y 
# "numeric" "numeric" "numeric" 

之后
n <- 10
p <- 6
set.seed(1)
x <- seq(0, 20, length.out = n) + rnorm(n, 0, 1)
y <- c(1:3)
y <- sample(y, n, replace = TRUE)
z <- 10*y*x + 10*y + 10 + rnorm(n, 0, 1)
y <- as.factor(y)
debug(lm)
fit <- lm(z ~ x*y)

mt <- attr(mf, "terms")看起来像

mt
# ...
# attr(,"dataClasses")
#         z         x         y 
# "numeric" "numeric"  "factor"

但是随后看来,他们总是打电话给lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...),那里的z <- .Call(C_Cdqrls, x, y, tol, FALSE)我认为只有在没有任何因素的情况下才有效。 上面的链接很好地解释了模型矩阵和qr分解的所有内容,我认为在有因素的情况下这是行不通的。

编辑x <- model.matrix(mt, mf, contrasts)之后的模型矩阵已经不同。如果是数字

x
   (Intercept)          x y       x:y
1            1 -0.6264538 3 -1.879361
2            1  2.4058655 1  2.405866
3            1  3.6088158 2  7.217632
4            1  8.2619475 1  8.261947
5            1  9.2183967 1  9.218397
6            1 10.2906427 2 20.581285
7            1 13.8207624 1 13.820762
8            1 16.2938803 2 32.587761
9            1 18.3535591 3 55.060677
10           1 19.6946116 2 39.389223
attr(,"assign")
[1] 0 1 2 3

在有因素的情况下

x
   (Intercept)          x y2 y3      x:y2       x:y3
1            1 -0.6264538  0  1  0.000000 -0.6264538
2            1  2.4058655  0  0  0.000000  0.0000000
3            1  3.6088158  1  0  3.608816  0.0000000
4            1  8.2619475  0  0  0.000000  0.0000000
5            1  9.2183967  0  0  0.000000  0.0000000
6            1 10.2906427  1  0 10.290643  0.0000000
7            1 13.8207624  0  0  0.000000  0.0000000
8            1 16.2938803  1  0 16.293880  0.0000000
9            1 18.3535591  0  1  0.000000 18.3535591
10           1 19.6946116  1  0 19.694612  0.0000000
attr(,"assign")
[1] 0 1 2 2 3 3
attr(,"contrasts")
attr(,"contrasts")$`y`
[1] "contr.treatment"

编辑2 :部分问题也可以找到here

1 个答案:

答案 0 :(得分:0)

借助this question的答案,我意识到答案很简单:

如果因子属于变量(预测变量),则model.matrix会变大。显然,C_Cdqrls可以处理模型矩阵。

仅当因变量包含因子时,线性回归或lm不能正常工作,判别分析是一种可能。 (乍一看,stats::glm使用了logit模型。

来自Wikipedia

  

判别函数分析与逻辑回归非常相似,   两者都可以用来回答相同的研究问题。   Logistic回归没有那么多的假设和限制   作为判别分析。但是,当进行判别分析时   满足假设,它比逻辑回归更强大。   与逻辑回归不同,判别分析可用于   小样本量。已经证明,当样本量为   相等,并且方差/协方差的同质性成立,可判别   分析更准确。考虑到所有这些,后勤   回归已经成为常见的选择,因为   判别分析很少见。


示例:

x <- seq(0, 10, length.out = 21)
y <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
y <- as.factor(y)
df <- data.frame(x = x, y = y)

# see ??numeric and the ‘Warning’ section in factor:
plot(x, as.numeric(levels(y))[y], ylim = c(0, 1.2))

fit <- lm(y ~ x, data = df)
print(summary(fit))

fit_glm <- stats::glm(y ~ x, family = binomial(link = "logit"), data = df, control = list(maxit = 50))
print(summary(fit_glm))

df$glm.probs <- stats::predict(fit_glm, newdata = df, type = "response")
df$glm.pred = ifelse(glm.probs > 0.5, 1, 0)
points(x, df$glm.pred + 0.05, col = "red")