Reputation: 7755
I am trying to write a loop, which returns linear regression and correlation parameters. When trying to pass substitute through cor.test
function, I encounter an unexpected error
data(iris)
i <- 1
vars <- list(par = as.name(colnames(iris)[1]), expl = as.name(colnames(iris)[2:4][i]))
lm(substitute(par ~ expl, vars), data = iris) # works
lm(Sepal.Length ~ Sepal.Width, data = iris) # works. Result equal to the statement above
cor.test(~Sepal.Length + Sepal.Width, data = iris) # works
cor.test(substitute(~par + expl, vars), data = iris) # does not work
## Error in cor.test.default(substitute(~par + expl, vars), data = iris) :
## argument "y" is missing, with no default
To my understanding the cor.test statement should be the same than the manually inputted one.
What is the reason for the error? How can I write a substitute statement for cor.test
that works?
Upvotes: 0
Views: 770
Reputation: 522
The error stems from the fact that the first version is of formula
type and the second one is language
:
str(substitute(~par + expl, vars))
# language ~Sepal.Length + Sepal.Width
str(~Sepal.Length + Sepal.Width)
# Class 'formula' language ~Sepal.Length + Sepal.Width
# ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
If you use as.formula
on the second version it works:
cor.test(as.formula(substitute(~par + expl, vars)), data = iris)
# Pearson's product-moment correlation
#
# data: Sepal.Length and Sepal.Width
# t = -1.4403, df = 148, p-value = 0.1519
# alternative hypothesis: true correlation is not equal to 0
# 95 percent confidence interval:
# -0.27269325 0.04351158
# sample estimates:
# cor
# -0.1175698
Upvotes: 1