Hi,
I have been desperately trying to create a function that uses a glm() inside it. But I always get an error message. It looks like the function does not retrieve the value of the variable. Any help would be greatly appreciated. Not sure how to solve this
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
Hi @simRock. The error from the argument dep that you pass the variable age which is not available. You can change the function code and call as follow.
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(formula(paste0(dep, "~factor(", indep, ")")), data=data)
}
to_analyze(dep="age", indep="sex", data=dsn)
#>
#> Call: glm(formula = formula(paste0(dep, "~factor(", indep, ")")), 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
After running your code, look what's in the namespace
ls()
[1] "dsn" "to_analyze"
As @raytong notes , the function invocation doesn't provide for looking inside the function to discover that the third argument is a data frame with the first and second arguments as variables within it.
You can simply things somewhat by relying on positional argument
However, I am stuck again because I am trying the call the output from this model in lsmeans::lsmeans() to predict marginal means and return the output but it is giving me an error. Any help would be greatly appreciated
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 found