I've made this function which is intended to output the summary of an ANCOVA and plot the results:
statAncova <- function (dataframe, response, covariate, Factor) {
library(ggplot2)
mod <- aov(response ~ covariate + Factor, data=dataframe)
pred <- predict(mod)
plotMod <- ggplot(data = cbind(mod$model, pred), aes(covariate, response, color=Factor)) +
geom_point() +
facet_grid(. ~ Factor) +
geom_line(aes(y=pred))
return(list(mod, plotMod))
}
If I try to use function like this:
statAncova(mtcars, drat, hp, cyl)
I get this error:
Error in eval(expr, envir, enclos) : object 'drat' not found
What am I doing wrong?
response,covariateorFactorat all. You'll have to pass them as character strings, and construct any formulas by hand. - joran