Tukey Test following ANOVA test

I am trying to run a Tukey test in order to determine how total income state tax differs across political parties of governor after conducting my ANOVA test. However, when input the Tukey test code it comes up with an error message saying "no applicable method for 'TukeyHSD' applied to an object of class "data.frame"".
I have attached my code would anyone be able to help me with this?

Run preamble

rm(list=ls())

install packages
library(tidyverse)
library(kableExtra)
library(ggplot2)
library(psych)

Open dataset

cw2dataset <- read.csv("CW2_Dataset.csv")

Select relevant variables

merged <- cw2dataset %>% dplyr::select(aiinct, govparty_a)

ANOVA test

income_aov <- merged %>% dplyr::filter(govparty_a >= 0 | govparty_a <= 1)

summary(aov(govparty_a ~ aiinct, data = income_aov))

Run a Tukey test

tukey_result <- TukeyHSD(income_aov)

Here is part of the help file for the TukeyHSD() function

Usage
TukeyHSD(x, which, ordered = FALSE, conf.level = 0.95, ...)

Arguments
x	A fitted model object, usually an aov fit.

So, x should be an object produced by aov(). Try

Fit_aov <- aov(govparty_a ~ aiinct, data = income_aov))

tukey_result <- TukeyHSD(Fit_aov)

Thanks,
The Fit_aov line of code works however when I tried the tukey_result line it came up with an error saying "Error in TukeyHSD.aov(Fit_aov) : no factors in the fitted model"

As I mentioned in another thread (Help with a hypothesis test),
your aov() is testing whether govparty_a is predicted by aiinct. That seems backwards. Don't you want to test if aiinct is predicted by govparty_a? If so you need to change the formula in aov() to aiinct ~ govparty_a.

You're right apologies that was my mistake. I have swapped them round in the aov() formula however for some reason it still does not let me run the tukeyHSD command and comes up with the same error.

My first guess is that govparty_a is not a factor. You can check that with class(incone_aov$govparty_a). If it is not a factor, you can make it one with

income_aov$govparty_a <- factor(income_aov$govparty_a, labels = c("green", "yellow"))

changing the party labels to whatever makes sense.

I ran the code to check and it confirms it is a factor saying it is [1] "numeric".

Could it be that there are some values in govparty_a which are not a 1 or a 0. I knew this might present an issue somewhere but could not figure out how to remove them or assign them to one of the values.

The class() function returned "numeric", showing that govparty_a is not a factor. Here is an example of a numeric column converted to a factor.

DF <- data.frame(A = c(0,1,0,0,1,1))
DF
  A
1 0
2 1
3 0
4 0
5 1
6 1
class(DF$A)
[1] "numeric"
DF$A <- factor(DF$A, labels = c("yellow","green"))
class(DF$A)
[1] "factor"
DF
       A
1 yellow
2  green
3 yellow
4 yellow
5  green
6  green

okay ill give this a go thanks alot for your help

This topic was automatically closed 42 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.