Input in predict(), wrapped in a user-defined function - R -


i trying write user-defined function, includes predict() statement. in predict() statement, have specify newdata = ..., , here have specify data want use dependent , independent variables. however, if pass function-arguments (of my_fun) predict(), error

error in eval(expr, envir, enclos) : object 'x' not found 

does have clue of how can use function arguments in predict() statement? have tried deparse(substitute(iv)), didn't work.

please see function wrote below.

my_fun <- function(riskdrivers, iv, dv, df) {    #fit model   form <- formula(paste(dv, "~", iv, "+", paste(riskdrivers, collapse="+")))   fit <- glm(form, family=binomial(link='logit'), data=df)   summary(fit)    #get predicted values   df$pred <- predict(fit, newdata = data.frame(iv==df[, iv], riskdrivers[1]==df[ ,riskdrivers[1]], riskdrivers[2]==df[, riskdrivers[2]]), type="response") }  my_fun(riskdrivers = c("a", "b"), iv = "x", dv = "y", df = mydf) 


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -