编写自定义函数:eval(expr,envir,enclos)中的错误:找不到对象

时间:2016-05-17 23:08:12

标签: r rstudio

我正在尝试编写自定义函数。我有工作代码:

m <- lm(age ~ religion * degree * country + gender, WVS)
e_df <- data.frame(e <- effect("religion:degree:country", m))
e_df$predictor <- paste(e_df[,3], e_df[,2], e_df[,1], sep="\n\n") # merge first three columns
ifelse(names(e_df)=="fit", "age", names(e_df))->names(e_df) # rename column 1
print(subset(e_df, select = c(predictor, age, se)), digits = 3)

我尝试将其写入自定义函数:

threewaycovar<- function(outcome, V1, V2, V3, covar, d) 
{require(effects)
m <- lm(outcome ~ V1 * V2 * V3 + covar, d)
e <- effect("V1:V2:V3", m)
e_df <- data.frame(e)
e_df$predictor <- paste(e_df[,3], e_df[,2], e_df[,1], sep="\n\n") # merge     first three columns
ifelse(names(e_df)=="fit", "outcome", names(e_df))->names(e_df) # rename column 1
op <-(subset(e_df, select = c(predictor, outcome, se)))
return(op)}

但是,自定义功能一直给我错误信息

  

&#34; eval(expr,envir,enclos)中的错误:对象&#39;结果&#39;找不到&#34;。

我不知道为什么我收到此错误消息。 我尝试使用

运行它
threewaycovar(WVS$age, WVS$religion, WVS$degree, WVS$country, WVS$gender, WVS)

我看到了这篇文章Error in eval(expr, envir, enclos) - contradiction?,并且提到了与结果相关的列号,但我的功能仍然不起作用。谁能告诉我为什么我的功能不起作用?

编辑以回应评论: WVS是R中包含的默认包,因此我的数据是可重现的。我的追溯是

14 eval(expr, envir, enclos) 
13 eval(predvars, data, env) 
12 model.frame.default(outcome ~ V1 * V2 * V3 + covar + (V1 + V2 + 
V3 + covar), data = structure(list(poverty = structure(c(1L, 2L, 1L, 3L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 3L, 3L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 3L, 1L,  ... 
11 model.frame(outcome ~ V1 * V2 * V3 + covar + (V1 + V2 + V3 + 
covar), data = structure(list(poverty = structure(c(1L, 2L, 1L, 3L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 3L, 3L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 3L, 1L, 1L,  ... 
10 eval(expr, envir, enclos) 
9 eval(call("model.frame", ff, data = data, subset = subset, na.action = naa), 
envir) 
8 expand.model.frame(mod, all.predictors) 
7 na.omit(expand.model.frame(mod, all.predictors)) 
6 Analyze.model(focal.predictors, mod, xlevels, default.levels, 
formula.rhs, partial.residuals = partial.residuals, quantiles = quantiles, 
x.var = x.var, data = data, typical = typical) 
5 Effect.lm(predictors, mod, vcov. = vcov., ...) 
4 Effect(predictors, mod, vcov. = vcov., ...) 
3 effect.default("V1:V2:V3", m) 
2 effect("V1:V2:V3", m) 
1 threewaycovar(WVS$age, WVS$religion, WVS$degree, WVS$country, 
WVS$gender, WVS) 

我正在尝试创建一个函数,它将为我提供至少一个协变量的三向交互控制的估计均值和标准误差。以上是我到目前为止所尝试的内容。

1 个答案:

答案 0 :(得分:3)

您遇到的主要问题是您不能将变量抛出到公式或表达式中。这里重写了函数,您将列名称作为字符串而不是向量值传递。

library(effects)
threewaycovar <- function(outcome, effects, covar, d) {
    stopifnot(all(c(outcome, effects, covar) %in% names(d)))
    form <- as.formula(paste(outcome,"~", paste(effects,collapse="*"), "+", paste(covar, collapse="+")))
    m <- lm(form, d)
    e <- effect(paste(effects, collapse=":"), m)
    e_df <- data.frame(e)
    e_df$predictor <- apply(e_df[,rev(seq_along(effects))], 1, paste, collapse="\n\n") # merge     first three columns
    ifelse(names(e_df)=="fit", "outcome", names(e_df))->names(e_df) # rename column 1
    op <-e_df[,c("predictor", "outcome", "se")]
    return(op)
}

threewaycovar("age", c("religion","degree","country"), "gender", WVS)

这里我们以字符串形式动态构建公式,保留数据集中的原始名称。