我一直在尝试创建一个函数,其中使用glm()。但我总是收到一条错误信息。函数似乎没有检索变量的值。
set.seed(234)
sex <- sample(c("M", "F"), size=100, replace=TRUE)
age <- rnorm(n=100, mean=20 + 4*(sex=="F"), sd=0.1)
dsn <- data.frame(sex, age)
rm(sex, age) #remove sex and age from the global environment for reproducibility
to_analyze <- function(dep, indep, data){
glm(dep~factor(indep), data=data)
}
to_analyze(dep=age, indep=sex, data=dsn)
#> Error in eval(predvars, data, env): object 'age' not found发布于 2020-02-28 06:33:15
您可以使用以下任何一种方法:
使用
substitute:
to_analyze <- function(dep, indep, data){
glm(substitute(dep ~ factor(indep)), data=data)
}
to_analyze(dep=age, indep=sex, data=dsn)优点:能写出独立的公式。
例如
to_analyze(Petal.Width, Sepal.Length + Sepal.Width, data = iris)使用@NelsonGon所述的重格式
to_analyze <- function(dep, indep, data){
glm(reformulate(sprintf("factor(%s)",indep), dep), data = data)
}注意,要调用这个函数,变量应该是字符类型的。
to_analyze(dep= "age", indep="sex", data=dsn)Recall
glm也可以接受可以解析为公式的字符串:
to_analyze <- function(dep, indep, data){
glm(sprintf("%s~factor(%s)", dep, indep), data = data)
}
to_analyze("age", "sex", data=dsn)甚至:
to_analyze <- function(dep, indep, data){
glm(paste(dep,"~ factor(",indep,")"), data = data)
}
to_analyze("age", "sex", data=dsn)最后:将替代和粘贴结合起来:
to_analyze <- function(dep, indep, data){
glm(paste(substitute(dep),"~ factor(",substitute(indep),")"), data = data)
}将同时适用于符号和字符。例:
to_analyze(age, sex, data=dsn)
to_analyze("age", "sex", data=dsn)发布于 2020-02-28 05:27:41
在函数中创建一个"formula"对象并传递给glm。
要在不给出错误的情况下获得变量,标准的技巧是deparse(substitute(.))。
然后用paste编写公式。
to_analyze <- function(dep, indep, data){
dep <- deparse(substitute(dep))
indep <- deparse(substitute(indep))
indep <- paste0("factor(", indep, ")")
fmla <- paste(dep, indep, sep = " ~ ")
fmla <- as.formula(fmla)
glm(fmla, data = data)
}
to_analyze(dep=age, indep=sex, data=dsn)
#
#Call: glm(formula = fmla, data = data)
#
#Coefficients:
# (Intercept) factor(sex)M
# 23.984 -3.984
#
#Degrees of Freedom: 99 Total (i.e. Null); 98 Residual
#Null Deviance: 396.2
#Residual Deviance: 0.837 AIC: -188.5发布于 2020-02-29 00:03:13
@Onyambu和其他人。替代命令似乎只适用于一个调用,因为它适用于to_analyze()。然而,当我调用它内部的另一个函数时,它又在抱怨。任何帮助都将不胜感激。
to_analyze <- function(dep, indep, data){
glm(substitute(dep ~ factor(indep)), data=data)
}
to_analyze(dep=age, indep=sex, data=dsn)
#>
#> Call: glm(formula = substitute(dep ~ factor(indep)), data = data)
#>
#> Coefficients:
#> (Intercept) factor(sex)M
#> 24.006 -4.034
#>
#> Degrees of Freedom: 99 Total (i.e. Null); 98 Residual
#> Null Deviance: 397.3
#> Residual Deviance: 0.8152 AIC: -191.2然而,我再次陷入困境,因为我试图在lsmeans::lsmeans()中调用这个模型的输出来预测边际均值并返回输出,但这给了我一个错误。虽然它不需要一个偏移量,但我在这里包括它,以便我可以得到一个更一般的代码,我可以修改以后。任何帮助都将不胜感激。
to_predict_lsmeans <- function(dep, indep, data){
model <- glm(substitute(dep ~ factor(indep)), data=data)
pred <- lsmeans:: lsmeans(model, substitute(~ factor(indep)), offset=substitute(data)$log(age), type ="response" )
return(pred)
}
pred <- to_predict_lsmeans(dep=age, indep=sex, data=dsn)
#> Error in ref_grid(object, ...): We are unable to reconstruct the data.
#> The variables needed are:
#> sex
#> Are any of these actually constants? (specify via 'params = ')
#> The dataset name is:
#> data
#> Does the data still exist? Or you can specify a dataset via 'data = '
pred
#> Error in eval(expr, envir, enclos): object 'pred' not foundhttps://stackoverflow.com/questions/60445660
复制相似问题