1 Loading Libraries

#install.packages("afex")
#install.packages("emmeans")
#install.packages("ggbeeswarm")
#install.packages("expss")

library(psych) # for the describe() command
library(ggplot2) # to visualize our results
library(expss) # for the cross_cases() command
library(car) # for the leveneTest() command
library(afex) # to run the ANOVA 
library(ggbeeswarm) # to run plot results
library(emmeans) # for posthoc tests

2 Importing Data

# For HW, import the project dataset you cleaned previously this will be the dataset you'll use throughout the rest of the semester

d <- read.csv(file="Data/projectdata.csv", header=T)


# new code! this adds a column with a number for each row. It will make it easier if we need to drop outliers later
d$row_id <- 1:nrow(d)

Note: For your HW, you will choose to run EITHER a one-way ANOVA (a single IV with 3 or more levels) OR a two-way/factorial ANOVA (two IVs with 2 or 3 levels each). You will need to specify your hypothesis and customize your code based on the choice you make. We will run BOTH versions of the test in the lab for illustrative purposes.

3 One-Way ANOVA

3.1 State Your Hypothesis

We predict that there will be a significant difference in social support by people’s level of pet ownership between those who do not own pets, those who own a cat, and those who own a dog.

3.2 Check Your Variables

# you only need to check the variables you're using in the current analysis

str(d)
## 'data.frame':    666 obs. of  8 variables:
##  $ X                  : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ relationship_status: chr  "Single, never married" "Single, never married" "Prefer not to say" "Single, never married" ...
##  $ pet                : chr  "cat" "other" "no pets" "no pets" ...
##  $ mfq_26             : num  2.7 4.55 4.8 3.8 4.5 4 5.8 4.2 4.5 5.25 ...
##  $ rse                : num  2.6 3.1 3.7 3 3 3 4 3.8 2.5 4 ...
##  $ phq                : num  1.56 1.44 1.11 1.33 1.44 ...
##  $ support            : num  2.83 3 4 4 3.67 ...
##  $ row_id             : int  1 2 3 4 5 6 7 8 9 10 ...
# make our categorical variables of interest "factors"
# because we'll use our newly created row ID variable for this analysis, so make sure it's coded as a factor, too.
d$pet <- as.factor(d$pet) 
d$row_id <- as.factor(d$row_id)

# check that our categorical variables of interest are now factors
str(d)
## 'data.frame':    666 obs. of  8 variables:
##  $ X                  : int  520 2814 3146 3295 717 6056 4753 5365 2044 1965 ...
##  $ relationship_status: chr  "Single, never married" "Single, never married" "Prefer not to say" "Single, never married" ...
##  $ pet                : Factor w/ 8 levels "bird","cat","cat and dog",..: 2 8 7 7 7 7 7 2 2 7 ...
##  $ mfq_26             : num  2.7 4.55 4.8 3.8 4.5 4 5.8 4.2 4.5 5.25 ...
##  $ rse                : num  2.6 3.1 3.7 3 3 3 4 3.8 2.5 4 ...
##  $ phq                : num  1.56 1.44 1.11 1.33 1.44 ...
##  $ support            : num  2.83 3 4 4 3.67 ...
##  $ row_id             : Factor w/ 666 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
table(d$pet)
## 
##                  bird                   cat           cat and dog 
##                     3                   119                    80 
##                   dog                  fish multiple types of pet 
##                   106                    20                    57 
##               no pets                 other 
##                   241                    40
d <- subset(d, pet != "bird") # use subset() to remove all participants from the additional level
d$pet <- droplevels(d$pet) # use droplevels() to drop the empty factor

d <- subset(d, pet != "cat and dog") # use subset() to remove all participants from the additional level
d$pet <- droplevels(d$pet) # use droplevels() to drop the empty factor

d <- subset(d, pet != "fish") # use subset() to remove all participants from the additional level
d$pet <- droplevels(d$pet) # use droplevels() to drop the empty factor

d <- subset(d, pet != "multiple types of pet") # use subset() to remove all participants from the additional level
d$pet <- droplevels(d$pet) # use droplevels() to drop the empty factor

d <- subset(d, pet != "other") # use subset() to remove all participants from the additional level
d$pet <- droplevels(d$pet) # use droplevels() to drop the empty factor


# check our DV skew and kurtosis
describe(d$support)
##    vars   n mean  sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 466  3.5 0.9    3.5    3.52 0.99 1.17   5  3.83 -0.22    -0.68 0.04
# we'll use the describeBy() command to view our DV's skew and kurtosis across our IVs' levels
describeBy(d$support, group = d$pet)
## 
##  Descriptive statistics by group 
## group: cat
##    vars   n mean   sd median trimmed  mad min max range  skew kurtosis   se
## X1    1 119 3.41 0.89    3.5    3.42 0.99 1.5   5   3.5 -0.09    -0.79 0.08
## ------------------------------------------------------------ 
## group: dog
##    vars   n mean   sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 106 3.47 0.91    3.5     3.5 0.99 1.33   5  3.67 -0.33    -0.64 0.09
## ------------------------------------------------------------ 
## group: no pets
##    vars   n mean  sd median trimmed  mad  min max range  skew kurtosis   se
## X1    1 241 3.55 0.9   3.67    3.58 0.99 1.17   5  3.83 -0.24    -0.68 0.06
# also use histograms to examine your continuous variable
hist(d$support)

3.3 Check Your Assumptions

3.3.1 ANOVA Assumptions

  • DV should be normally distributed across levels of the IV (we checked previously using “describeBy” function)
  • All levels of the IVs should have an equal number of cases and there should be no empty cells. Cells with low numbers decrease the power of the test (which increases chance of Type II error)
  • Homogeneity of variance should be confirmed (using Levene’s Test)
  • Outliers should be identified and removed – we will actually remove them this time!
  • If you have confirmed everything above, the sampling distribution should be normal.

3.3.2 Check levels of IVs

table(d$pet)
## 
##     cat     dog no pets 
##     119     106     241
# REMEMBER your test's level of POWER is determined by your SMALLEST subsample

3.3.3 Check for outliers using Cook’s distance and Residuals VS Leverage plot

3.3.3.1 Run a Regression to get both outlier plots

# use this commented out section below ONLY IF if you need to remove outliers
# to drop a single outlier, use this code:
d <- subset(d, row_id!=c(1108))  # NOTE: Participant 1108 is not truly problematic in the Lab, but we are going to removed them for demonstration.

# to drop multiple outliers, use this code:
# d <- subset(d, row_id!=c(1108) & row_id!=c(602))


# use the lm() command to run the regression
# formula is y~x, where y is our DV, x is our IV

reg_model <- lm(support~pet, data = d) 

3.3.3.2 Check for outliers

# Cook's distance
plot(reg_model, 4)

# Residuals VS Leverage
plot(reg_model, 5)

# IF you find outliers, go back up to line 114 or 117 and remove it/them, then re-run the reg_model code

3.3.4 Check homogeneity of variance

# use the leveneTest() command from the car package to test homogeneity of variance
# uses the 'formula' setup: formula is y~x, where y is our DV and x is our IV 
leveneTest(support~pet, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   2  0.0584 0.9432
##       463

3.3.5 Issues with My Data

Our cell sizes are somewhat between the pet type group levels. The smallest sample size for one of the levels of our variable (dog, n = 106) limits our power and increases our Type II error rate.

Levene’s test was not significant for our three-level pet ownership variable and met homogeneity assumptions. We will be contnuing with the One-Way ANOVA.

We did not identify or have to remove any outliers for the One-Way ANOVA.

3.4 Run a One-Way ANOVA

aov_model <- aov_ez(data = d,
                    id = "X",
                    between = c("pet"),
                    dv = "support",
                    anova_table = list(es = "pes"))
## Contrasts set to contr.sum for the following variables: pet

3.5 View One-Way Output

nice(aov_model)
## Anova Table (Type 3 tests)
## 
## Response: support
##   Effect     df  MSE    F  pes p.value
## 1    pet 2, 463 0.82 1.09 .005    .337
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '+' 0.1 ' ' 1

ANOVA Effect Size [partial eta-squared] cutoffs from Cohen (1988): * η^2 < 0.01 indicates a trivial effect * η^2 >= 0.01 indicates a small effect * η^2 >= 0.06 indicates a medium effect * η^2 >= 0.14 indicates a large effect

3.6 Visualize One-Way Results

afex_plot(aov_model, x = "pet")

3.7 Write Up One-Way ANOVA Results

To test our hypothesis that there will be a significant difference in people’s level of social support based on the pet ownership (dog, cat, no pets), we used a one-way ANOVA. Our data was unbalanced, with more people who do not own a pet participating in our survey (n = 241) than who own a cat (n = 119) or a dog (n = 106). This significantly reduces the power of our test and increases the chances of a Type II error. Following visual analysis of Cook’s Distance and Residual VS Leverage plots we did not identify or remove any outliers. A non-significant Levene’s test (p = .943) also indicates that our data meets the assumption of homogeneity of variance.

Contrary to our prediction, the three pet ownership groups did not significantly differ on social support, F(2, 463) = 1.09, p = .337, ηp2 = .005 indicating a trivial effect (Cohen, 1988). Descriptively, cat owners reported a mean social support score of M = 2.39 (SD = 0.74), dog owners reported M = 2.46 (SD = 0.69), and participants with no pets reported M = 2.57 (SD = 0.70). Figure 1 illustrates that social support did not significantly differ across pet ownership groups. Because the ANOVA was not significant, Posthoc tests were not conducted.

References

Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.