Reputation: 1000
I have a data frame that looks like the following:
goal behavior dv1 dv2 dv3
1 1 4 2 6
1 5 3 5 1
1 7 2 4 2
5 1 5 2 7
5 5 2 1 3
5 7 2 1 2
7 1 4 5 7
7 5 2 1 4
7 7 3 2 5
As you can see, there are 9 conditions (3 possibilities for goal x 3 possibilities for behavior). In the actual data set, there are approximately 25-30 participants for each of the 9 conditions. I am trying to run two-way anova's on the dependent variables (dv1
, dv2
, and dv3
) in this example to see if there is an interaction between goal
and behavior
.
I have tried running the following code. dv
is a matrix that just includes columns dv1
, dv2
, dv3
. df
is the entire data frame above.
output <- lm(as.matrix(dv) ~ goal * behavior, data = df,
contrasts = list(goal = contr.sum, behavior = contr.sum))
summary(aov(output))
When I try to create the object output
, I get the following error:
Error in
contrasts<-
(*tmp*
, value = contrasts.arg[[nn]]) :
contrasts apply only to factors
I have looked at this question but am still unable to solve my problem. When I run the following code, I am told that none of my variables are considered to be factors. I am not sure why this is when both "goal" and "behavior" have 3 levels each with variation.
factor <- sapply((df), function(x) is.factor(x))
The other questions I've looked at on here specifically address the error message of
contrasts apply only to factors with two or more levels
which is different than the error message I received. What can I do to get this to run properly?
Upvotes: 0
Views: 1347
Reputation: 226771
"factor" has a specific meaning in R (e.g. see this page from UC Berkeley); they're not just any variable that varies, but variables that are encoded so that R knows it's supposed to treat them as categorical predictors.
In your case,
df <- transform(df, goal=factor(goal), behavior=factor(behavior))
should work (factor()
and as.factor()
are approximately the same, use whichever you prefer). The suggestion from comments (df <- df %>% transmute_all(as.factor)
) is a little more compact, but (1) depends on the "tidyverse" suite of packages (which you might not prefer) and (2) changes all variables within the data frame to factor (ditto).
Upvotes: 2