r - 在计算逐步多项式回归之前如何消除p值> 0.7的变量?

标签 r regression non-linear-regression p-value poly

我尝试使用 AIC(通过 step)对 1,400 个变量运行逐步回归,但我的计算机死机了。如果我包含 <300 个变量(运行 13 小时后),它就会起作用。

在运行逐步回归之前,有没有办法消除一些变量(如果 p 值 >.7)?

# Polynomial Regression
REG19 <- lm(R10 ~ poly(M1, 3) + poly(M2, 3) + poly(M3, 3), WorkData)

# Is there a way to get rid of variables with 
# p values >.7 at this point of the code?

# Beginning of stepwise regression
n <- length(resid(REG19))
REG20 <- step(REG19, direction="backward", k=log(n))

最佳答案

您可能想要排除有关最高多项式的任何内容,其中 p <= .7 (应保留较低的度数)。假设您知道自己在做什么,您可以编写一个函数 degAna()分析每个多项式的次数并将其应用于 summary 获得的系数矩阵.

REG19 <- lm(R10 ~ poly(M1, 3) + poly(M2, 3) + poly(M3, 3) + poly(M4, 3) +
              poly(M5, 3) + poly(M6, 3) + poly(M7, 3) + poly(M8, 3) + 
              poly(M9, 3) + poly(M10, 3), WorkData)

rr <- summary(REG19)$coefficients

检测最高程度的函数p <= .7 :

degAna <- function(d) {
  out <- as.matrix(rr[grep(paste0(")", d), rownames(rr)), "Pr(>|t|)"] <= .7)
  dimnames(out) <- list(c(gsub("^.*\\((.*)\\,.+", "\\1", rownames(out))), d)
  return(out)
}

lapply degAna到系数矩阵:

dM <- do.call(cbind, lapply(1:3, degAna))  # max. degree always 3 as in example
#         1     2     3
# M1   TRUE  TRUE  TRUE
# M2   TRUE  TRUE  TRUE
# M3  FALSE  TRUE  TRUE
# M4   TRUE  TRUE  TRUE
# M5   TRUE  TRUE  TRUE
# M6   TRUE FALSE  TRUE
# M7   TRUE FALSE FALSE
# M8   TRUE  TRUE  TRUE
# M9   TRUE  TRUE FALSE
# M10  TRUE FALSE  TRUE

现在我们需要多项式的最后一次,其中 p <= .7 :

tM <- apply(dM, 1, function(x) max(which(x != 0)))
tM <- tM[tM > 0]  # excludes polynomes where every p < .7
# M1  M2  M3  M4  M5  M6  M7  M8  M9 M10 
#  3   3   3   3   3   3   1   3   2   3 

(请注意,如果多项式完全具有 apply ,即行完全具有 p <= .7FALSE 将引发警告。由于我们在下一行中将它们扔掉,所以我们可以忽略 apply(dM, 1, function(x) suppressWarnings(max(which(x != 0)))) 的警告。 .)

有了这些信息,我们就可以用 reformulate 拼凑出一个新公式。 ,

terms.new <- paste0("poly(", names(tM), ", ", tM, ")")
FO <- reformulate(terms.new, response="R10")
# R10 ~ poly(M1, 3) + poly(M2, 3) + poly(M3, 3) + poly(M4, 3) + 
#     poly(M5, 3) + poly(M6, 3) + poly(M7, 1) + poly(M8, 3) + poly(M9, 
#     2) + poly(M10, 3)

用它我们最终可以进行所需的缩短回归。

REG19.2 <- lm(FO, WorkData)

n <- length(resid(REG19.2))
REG20.2 <- step(REG19.2, direction="backward", k=log(n))
# [...]

模拟数据

set.seed(42)
M1 <- rnorm(1e3)
M2 <- rnorm(1e3)
M3 <- rnorm(1e3)
M4 <- rnorm(1e3)
M5 <- rnorm(1e3)
M6 <- rnorm(1e3)
M7 <- rnorm(1e3)
M8 <- rnorm(1e3)
M9 <- rnorm(1e3)
M10 <- rnorm(1e3)
R10 <- 6 + 5*M1^3 + 4.5*M2^3 + 4*M3^2 + 3.5*M4 + 3*M5 + 2.5*M6 + 2*M7 + 
  .5*rnorm(1e3, 1, sd=20)
WorkData <- data.frame(M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, R10)

关于r - 在计算逐步多项式回归之前如何消除p值> 0.7的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56616301/

相关文章:

r - 范围-如果存在具有相同名称的本地var,如何在父环境中评估对象?

r - 在带有变量的 2 行上使用 melt 或 pivot_longer

r - 如何在R中使用plotly水平居中饼图?

c++ - 为什么 RcppArmadillo 的 fastLmPure 在输出中产生 NA 但 fastLm 没有?

python - 为什么使用 Seaborn 绘制回归时截距显示不正确?

r - 使用循环查找列表之间的匹配单词

machine-learning - 处理与输出预测不相关的特征?

r - R中的非线性回归分析

python - 拟合多项式的 Keras 模型

machine-learning - 机器学习中的连续回归