如何从函数外部将 lm 对象中的权重作为变量传递,并将列名作为模型表单中的权重引用?

How to pass weights in lm object as a variable from outside the function and refer the column name as weight in the model form?

我有一个函数可以为多个模型迭代创建 lm 对象,权重是该函数的输入,它采用不同的列值。

这是假人:

x <-c(rnorm(10),NA)
df <- data.frame(y=1+2*x+rnorm(11)/2, x=x, wght1=1:11)

## Fancy weights as numeric vector

df$weight <- (df$wght1)^(3/4)
weight_var <- "weight"

model <-  lm(y~x,data=df,weights=df[, weight_var])

model$call[[4]]

看,model$call[[4]] returns df[, weight_var],我希望它改为 return 列 weight;这是对该变量的引用

假设我在数据中有 a、b、c、d、e 列,我想 运行 模型并检查权重可能是 d 还是 e。

因此,我将 if 语句定义为:

if (weight_var[[1]]=='') {
    model <- lm(formula = eqmodel, xdata)
  } else {
    model <- lm(formula = eqmodel, xdata, weights = xdata[,weight_var])
  }

其中 weight_var 可以是 de。这样当我们调用:

model$call[[4]]

输出是 de

然而,当我看到模型为:

Call:
lm(formula = eqmodel, data = xdata, weights = xdata[, weight_var])

我同意 eqmodel 是从函数外部指定的模型的方程式。但是,我希望权重在传递时为 d 或 e。有办法吗?

已更新

model$call[[i]] returns lm() 参数的值逐个字母,因此不仅 model$call[[4]] 看起来没有信息,而且 model$call[[ 2]] returns 公式名称而不是公式。下面来个小技巧,稍微改进一下。

x <-c(rnorm(10),NA)
df <- data.frame(y=1+2*x+rnorm(11)/2, x=x, wght1=1:11)

## Fancy weights as numeric vector

df$weight <- (df$wght1)^(3/4)
weight_var <- "weight"
eqmodel <- as.formula("y~x")
xdata <- df

### unprocessed:
if (weight_var[[1]]=='') {
  model <- lm(formula = eqmodel, xdata)
} else {
  model <- lm(formula = eqmodel, xdata, weights = xdata[,weight_var])
}
summary(model)
#Call:
#lm(formula = eqmodel, data = xdata, weights = xdata[, weight_var])

### a little trick:
if (weight_var[[1]]=='') {
  model <- lm(formula = eqmodel, xdata)
} else {
  model <- lm(formula = eqmodel, xdata, weights = xdata[,weight_var])
  model$call[[4]] <- weight_var[[1]]
}
model$call[[2]] <- eqmodel
summary(model)

#Call:
#lm(formula = y ~ x, data = xdata, weights = "weight")