如何将变量的正方形作为i(x^2)与循环添加在线性模型中?

发布于 2025-01-24 04:01:40 字数 412 浏览 3 评论 0原文

我生成了获取数据的东西,这些数据将所有列名添加到线性回归中,添加了它们的交互和该变量的第二度,并使用步骤函数来找到最佳的线性回归模型。我添加了所有列的名称和交互,但是在这里我有一个问题,添加了第二度变量。我使用上述代码,但

Error in (colnames(dpt)[i])^2 : non-numeric argument to binary operator" error

有人可以帮忙吗?我真的很感激。

for (i in colnames(dpt)) {
  lmod <- lmod %>% update(paste('. ~ . + I((colnames(dpt)[i])^2)'),
                          data = dpt)
}

I generated something that takes data, which adds all column names to the linear regression, adds their interactions and their second degree of that variable, and uses step function to finds out the best linear regression model. I added both all column names and interactions but here I have a problem adding a second degree of the variables. I use the above-given code but it gives an

Error in (colnames(dpt)[i])^2 : non-numeric argument to binary operator" error

Could anyone help? I really appreciate it.

for (i in colnames(dpt)) {
  lmod <- lmod %>% update(paste('. ~ . + I((colnames(dpt)[i])^2)'),
                          data = dpt)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情空心 2025-01-31 04:01:40

使用sprintf

fit <- lm(mpg ~ cyl, mtcars)

v <- names(mtcars)[-(1:2)]

res <- lapply(v, \(x) update(fit, sprintf('. ~ . + I(%s^2)', x)))

或者,如果您进入管道:

res <- lapply(v, \(x) fit |> update(sprintf('. ~ . + I(%s^2)', x)))

或者如果没有循环的

res <- vector(mode='list', length=length(v))
for (i in seq_along(v)) {
  res[[i]] <- fit |> update(sprintf('. ~ . + I(%s^2)', v[i]))
}

res
# [[1]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(disp^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(disp^2)  
# 3.543e+01   -2.246e+00   -2.115e-05  
# 
# 
# [[2]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(hp^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl      I(hp^2)  
# 3.705e+01   -2.641e+00   -2.374e-05  
# 
# 
# [[3]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(drat^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(drat^2)  
# 31.6655      -2.4568       0.2745  
# 
# 
# [[4]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(wt^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl      I(wt^2)  
# 35.8818      -1.9282      -0.3423  
# 
# 
# [[5]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(qsec^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(qsec^2)  
# 43.73138     -3.15264     -0.01285  
# 
# 
# [[6]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(vs^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl      I(vs^2)  
# 39.6250      -3.0907      -0.9391  
# 
# 
# [[7]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(am^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl      I(am^2)  
# 34.522       -2.501        2.567  
# 
# 
# [[8]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(gear^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(gear^2)  
# 36.03695     -2.76010      0.08013  
# 
# 
# [[9]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(carb^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(carb^2)  
# 37.55404     -2.74723     -0.04454  

Using sprintf.

fit <- lm(mpg ~ cyl, mtcars)

v <- names(mtcars)[-(1:2)]

res <- lapply(v, \(x) update(fit, sprintf('. ~ . + I(%s^2)', x)))

Or, if you are into pipes:

res <- lapply(v, \(x) fit |> update(sprintf('. ~ . + I(%s^2)', x)))

Or if you cannot do without the for loop:

res <- vector(mode='list', length=length(v))
for (i in seq_along(v)) {
  res[[i]] <- fit |> update(sprintf('. ~ . + I(%s^2)', v[i]))
}

Gives

res
# [[1]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(disp^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(disp^2)  
# 3.543e+01   -2.246e+00   -2.115e-05  
# 
# 
# [[2]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(hp^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl      I(hp^2)  
# 3.705e+01   -2.641e+00   -2.374e-05  
# 
# 
# [[3]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(drat^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(drat^2)  
# 31.6655      -2.4568       0.2745  
# 
# 
# [[4]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(wt^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl      I(wt^2)  
# 35.8818      -1.9282      -0.3423  
# 
# 
# [[5]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(qsec^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(qsec^2)  
# 43.73138     -3.15264     -0.01285  
# 
# 
# [[6]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(vs^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl      I(vs^2)  
# 39.6250      -3.0907      -0.9391  
# 
# 
# [[7]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(am^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl      I(am^2)  
# 34.522       -2.501        2.567  
# 
# 
# [[8]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(gear^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(gear^2)  
# 36.03695     -2.76010      0.08013  
# 
# 
# [[9]]
# 
# Call:
#   lm(formula = mpg ~ cyl + I(carb^2), data = mtcars)
# 
# Coefficients:
#   (Intercept)          cyl    I(carb^2)  
# 37.55404     -2.74723     -0.04454  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文