Trying to run a Tukey's Test, but I am getting an error

This is my first question here, so I hope I am formatting correctly. I am trying to run a Tukey's test following an ANOVA and keep getting an error.

ANOVA:
res.aov_distance <- aov(distance ~ sleep_category, data = sleep_analysis)

Result:
Df Sum Sq Mean Sq F value Pr(>F)
sleep_category 1 177 177.09 21.2 5.62e-06 ***
Residuals 387 3233 8.35

Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1

When I run the Tukey's test prompt after this, I get an error message:

TukeyHSD(res.aov_distance)

Error message:

TukeyHSD(res.aov_distance)
Error in TukeyHSD.aov(res.aov_distance) : no factors in the fitted model
In addition: Warning message:
In replications(paste("~", xx), data = mf) :
non-factors ignored: sleep_category

I don't understand this because the factor in the ANOVA was sleep category, and the ANOVA ran fine.

I'm new and tried to reprex to post here, but got an error. I'm hoping this is a common problem that someone recognizes. Thank you.

See the FAQ: How to do a minimal reproducible example reprex for beginners.

Do you see something like this when you perform str(sleep_analysis)?

str(warpbreaks)
#> 'data.frame':    54 obs. of  3 variables:
#>  $ breaks : num  26 30 54 25 70 52 51 26 67 18 ...
#>  $ wool   : Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
#>  $ tension: Factor w/ 3 levels "L","M","H": 1 1 1 1 1 1 1 1 1 2 ...

Yes, all of my data are numbers.

spec_tbl_df [389 x 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
user_id : num [1:389] 1 1 1 1 1 1 1 1 1 1 ... total_steps : num [1:389] 13162 10735 9762 12669 9705 ...
calories : num [1:389] 1985 1797 1745 1863 1728 ... distance : num [1:389] 8.5 6.97 6.28 8.16 6.48 ...
percent_of_the_day_active: num [1:389] 0.254 0.178 0.189 0.185 0.154 ... sleep_category : num [1:389] 1 2 2 1 3 1 2 1 2 2 ...

Sorry, that is horrible formatting.

spec_tbl_df [389 x 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ user_id                  : num [1:389] 1 1 1 1 1 1 1 1 1 1 ...
 $ total_steps              : num [1:389] 13162 10735 9762 12669 9705 ...
 $ calories                 : num [1:389] 1985 1797 1745 1863 1728 ...
 $ distance                 : num [1:389] 8.5 6.97 6.28 8.16 6.48 ...
 $ percent_of_the_day_active: num [1:389] 0.254 0.178 0.189 0.185 0.154 ...
 $ sleep_category           : num [1:389] 1 2 2 1 3 1 2 1 2 2 ...
1 Like

So, you are going to?

spec_tbl_df$sleep_category  <- as.factor(spec_tbl_df$sleep_category)
1 Like

OMG, that totally worked. Thank you so much for your help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.