简体   繁体   中英

Had a couple problems with One Way ANOVA in R

My dput is this:

structure(list(Year = 2006:2021, Month_USD = c(1160L, 1240L, 1360L, 1480L, 1320L, 1320L, 375L, 1600L, 2000L, 2000L, 1600L, 2240L, 1900L, 2300L, 2900L, 2300L), Degree = c("High School", "High School", "High School", "High School", "High School", "High School", "High School", "High School", "High School", "BA", "BA", "BA", "BA", "BA", "M.Ed", "M.Ed"), Country = c("USA", "USA", "USA", "USA", "USA", "USA", "DE", "USA", "USA", "USA", "USA", "USA", "PRC", "PRC", "PRC", "HK"), Job = c("Disher", "Prep", "Prep", "Prep", "Prep", "Prep", "Au Pair", "CSA", "Valet", "Valet", "Intake", "CM", "Teacher", "Teacher", "Teacher", "Student"), Median_Household_Income_US = c(4833L, 4961L, 4784L, 4750L, 4626L, 4556L, 4547L, 4706L, 4634L, 4873L, 5025L, 5218L, 5360L, 5725L, NA, NA), US_Home_Price_Index = c(183.24, 173.36, 152.56, 146.69, 140.64, 135.16, 143.88, 159.3, 166.5, 175.17, 184.51, 195.99, 204.9, 212.59, 236.31, NA)), class = "data.frame", row.names = c(NA, -16L))

So I ran a one-way ANOVA on this data and had a couple problems. First, when I ran the level function here:

data(Earnings_Year)
View(Earnings_Year)
set.seed(1234)
Earnings_Year %>% 
  sample_n_by(Degree,
              size=1)
levels(Earnings_Year$Degree)

For whatever reason the code above wont show the levels and just spits out "NULL." As far as I know, the levels should be "BA", "High School", and "M.Ed."

Another issue I had later was when I ran this. When I ran a generic Shapiro test there didnt seem to be the same issue until I grouped it:

Earnings_Year %>% 
  group_by(Degree) %>% 
  shapiro_test(Month_USD)

When I run it, it comes up with the following problem:

Error: Problem with `mutate()` column `data`.
i `data = map(.data$data, .f, ...)`.
x Problem with `mutate()` column `data`.
i `data = map(.data$data, .f, ...)`.
x sample size must be between 3 and 5000
Run `rlang::last_error()` to see where the error occurred.

Any insight on what went wrong would be appreciated. Overall, I ended up with a nice ANOVA boxplot at the end that seemed to indicate what I was looking for:

方差分析箱线图

As the error message suggests there are certain groups in your data which have less than 3 rows or more than 5000 rows.

We can check number of rows in each group using count .

library(dplyr)
library(rstatix)

df %>% count(Degree)

#       Degree n
#1          BA 5
#2 High School 9
#3        M.Ed 2

You can remove such groups and the code should work fine.

df %>%
  group_by(Degree) %>%
  filter(n() > 2) %>%
  shapiro_test(Month_USD)

# Degree      variable  statistic     p
#  <chr>       <chr>         <dbl> <dbl>
#1 BA          Month_USD     0.944 0.695
#2 High School Month_USD     0.887 0.185

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM