用于线性回归的显式公式(Explicit formula used in linear regression)

我有一个公式列表,我使用lapply和lm来创建一个回归模型列表。 但是,当我查看每个线性模型的call组件时,不会看到显式公式,而是看到我解析为线性模型的变量的名称。 例如,使用mtcars数据集:

temp_formula_list = list(as.formula(hp~1),as.formula(hp~cyl)) temp_fm_list = lapply(temp_formula_list, function(x) lm(data = mtcars, formula = x))

然后检查temp_fm_list[[2]]的call :

temp_fm_list[[2]]$call

lm(formula = x, data = mtcars)

当我希望它明确给予

lm(formula = hp~cyl, data = mtcars)

I have a list of formulas, and I use lapply and lm to create a list of regression models. However, when I look at the call component of each linear model, instead of seeing the explicit formula, I see the name of the variable I parsed into the linear model. E.g. using the mtcars dataset:

temp_formula_list = list(as.formula(hp~1),as.formula(hp~cyl)) temp_fm_list = lapply(temp_formula_list, function(x) lm(data = mtcars, formula = x))

Then examining the call of temp_fm_list[[2]]:

temp_fm_list[[2]]$call

gives

lm(formula = x, data = mtcars)

when I would like it to explicitly give

lm(formula = hp~cyl, data = mtcars)

最满意答案

您可以使用bquote对语言进行一些简单的计算来构建您的呼叫。

temp_fm_list = lapply(temp_formula_list, function(x) { lmc <- bquote( lm(data = mtcars, formula = .(x))) eval(lmc) }) temp_fm_list # Call: # lm(formula = hp ~ 1, data = mtcars) # # Coefficients: # (Intercept) # 146.7 # # # [[2]] # # Call: # lm(formula = hp ~ cyl, data = mtcars) # # Coefficients: # (Intercept) cyl # -51.05 31.96

注意

function(x) do.call('lm', list(formula = x, data = quote(mtcars))

也会工作


其他方法....

即使使用原始调用,也可以从与模型关联的terms对象中重新创建公式

例如

x <- hp ~ cyl lmo <- lm(formula = x, data = mtcars) formula(lmo) ## hp ~ cyl lmo$call # lm(formula = x, data = mtcars)

如果你愿意,你可以混淆这个call对象(虽然这是相当危险的做法)

# for example lmo$call$formula <- x lmo$call ## lm(formula = hp ~ cyl, data = mtcars) ## however you can create rubbish here too lmo$call$formula <- 'complete garbage' lmo$call ## lm(formula = "complete garbage", data = mtcars) # but, being careful, you could use it appropriately temp_fm_list = lapply(temp_formula_list, function(x) { oo <- lm(data = mtcars, formula = x) oo$call$formula <-x oo }) temp_fm_list # Call: # lm(formula = hp ~ 1, data = mtcars) # # Coefficients: # (Intercept) # 146.7 # # # [[2]] # # Call: # lm(formula = hp ~ cyl, data = mtcars) # # Coefficients: # (Intercept) cyl # -51.05 31.96

You can do some simple computing on the language using bquote to construct your call.

temp_fm_list = lapply(temp_formula_list, function(x) { lmc <- bquote( lm(data = mtcars, formula = .(x))) eval(lmc) }) temp_fm_list # Call: # lm(formula = hp ~ 1, data = mtcars) # # Coefficients: # (Intercept) # 146.7 # # # [[2]] # # Call: # lm(formula = hp ~ cyl, data = mtcars) # # Coefficients: # (Intercept) cyl # -51.05 31.96

Note that

function(x) do.call('lm', list(formula = x, data = quote(mtcars))

Would also work


Other approaches....

Even with your original call you can recreate the formula from the terms object associated with the model

eg

x <- hp ~ cyl lmo <- lm(formula = x, data = mtcars) formula(lmo) ## hp ~ cyl lmo$call # lm(formula = x, data = mtcars)

You can mess with this call object if you wish (although this is rather dangerous practice)

# for example lmo$call$formula <- x lmo$call ## lm(formula = hp ~ cyl, data = mtcars) ## however you can create rubbish here too lmo$call$formula <- 'complete garbage' lmo$call ## lm(formula = "complete garbage", data = mtcars) # but, being careful, you could use it appropriately temp_fm_list = lapply(temp_formula_list, function(x) { oo <- lm(data = mtcars, formula = x) oo$call$formula <-x oo }) temp_fm_list # Call: # lm(formula = hp ~ 1, data = mtcars) # # Coefficients: # (Intercept) # 146.7 # # # [[2]] # # Call: # lm(formula = hp ~ cyl, data = mtcars) # # Coefficients: # (Intercept) cyl # -51.05 31.96

更多推荐