# install any packages you have not previously used, then comment them back out.
#install.packages("car")
#install.packages("effsize")
library(psych) # for the describe() command
library(car) # for the leveneTest() command
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
library(effsize) # for the cohen.d() command
##
## Attaching package: 'effsize'
## The following object is masked from 'package:psych':
##
## cohen.d
d <- read.csv(file="Data/projectdata.csv", header=T)
# For the HW, you will import the project dataset you cleaned previously
# This will be the dataset you'll use for HWs throughout the rest of the semester
We predict that there will be a significant difference in anxiety by location, as measured by the Pandemic Anxiety Scale (pas_covid).
[Remember to revise the above hypothesis in you HW assignment.]
# you only need to check the variables you're using in the current analysis
## Checking the Categorical variable (IV)
str(d)
## 'data.frame': 356 obs. of 7 variables:
## $ X : int 1 31 33 57 86 113 133 143 179 190 ...
## $ urban_rural: chr "city" "town" "city" "village" ...
## $ mhealth : chr "none or NA" "none or NA" "none or NA" "anxiety disorder" ...
## $ pas_covid : num 3.22 4.22 3.22 4.56 3.67 ...
## $ phq : num 1.33 2.33 1.11 2.33 1.56 ...
## $ isolation_a: num 2.25 2.5 1.75 1.25 3 1 1 1.5 2.75 2.75 ...
## $ support : num 2.5 2.5 3.67 4.17 1.33 ...
# if the categorical variable you're using is showing as a "chr" (character), you must change it to be a factor -- using the next line of code (as.factor)
d$urban_rural <- as.factor(d$urban_rural)
table(d$urban_rural, useNA = "always")
##
## city isolated dwelling town village
## 60 8 163 125
## <NA>
## 0
## Checking the Continuous variable (DV)
# you can use the describe() command on an entire dataframe (d) or just on a single variable within your dataframe -- which we will do here
describe(d$pas_covid)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 356 3.19 0.69 3.22 3.19 0.66 1.33 5 3.67 0.02 -0.06 0.04
# also use a histogram to visualize your continuous variable
hist(d$pas_covid)
# use the describeBy() command to view the means and standard deviations by group
# it's very similar to the describe() command but splits the dataframe according to the 'group' variable
describeBy(d$pas_covid, group=d$urban_rural)
##
## Descriptive statistics by group
## group: city
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 60 3.17 0.63 3.17 3.17 0.49 1.56 4.89 3.33 0.11 0.41 0.08
## ------------------------------------------------------------
## group: isolated dwelling
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 8 2.94 0.65 2.83 2.94 0.58 1.89 4 2.11 0.05 -1.15 0.23
## ------------------------------------------------------------
## group: town
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 163 3.17 0.7 3.22 3.19 0.66 1.33 5 3.67 -0.15 -0.1 0.05
## ------------------------------------------------------------
## group: village
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 125 3.24 0.71 3.22 3.23 0.66 1.67 5 3.33 0.17 -0.32 0.06
# last, use a boxplot to examine your continuous and categorical variables together #where find SD
boxplot(d$pas_covid~d$urban_rural)
# If the IV has more than 2 levels, you must drop the additional levels so that you meet the first assumption of a t-test.
d <- subset(d, urban_rural != "village")
table(d$urban_rural, useNA = "always") #verify that now there are no participants in the removed level
##
## city isolated dwelling town village
## 60 8 163 0
## <NA>
## 0
d$urban_rural <- droplevels(d$urban_rural) # use droplevels() to drop the empty factor
table(d$urban_rural, useNA = "always") #verify that now the entire factor level is removed
##
## city isolated dwelling town <NA>
## 60 8 163 0
# If the IV has more than 2 levels, you must drop the additional levels so that you meet the first assumption of a t-test.
d <- subset(d, urban_rural != "isolated dwelling")
table(d$urban_rural, useNA = "always") #verify that now there are no participants in the removed level
##
## city isolated dwelling town <NA>
## 60 0 163 0
d$urban_rural <- droplevels(d$urban_rural) # use droplevels() to drop the empty factor
table(d$urban_rural, useNA = "always") #verify that now the entire factor level is removed
##
## city town <NA>
## 60 163 0
We can test whether the variances of our two groups are equal using Levene’s test. The null hypothesis is that the variance between the two groups is equal, which is the result we want. So when running Levene’s test we’re hoping for a non-significant result!
# use the leveneTest() command from the car package to test homogeneity of variance
# it uses the same 'formula' setup that we'll use for our t-test: formula is y~x, where y is our DV and x is our IV
leveneTest(pas_covid~urban_rural, data = d)
## Levene's Test for Homogeneity of Variance (center = median)
## Df F value Pr(>F)
## group 1 1.7805 0.1835
## 221
As you can see, the data does not has significantly different variances between the two comparison groups.
[Revise the above statement for you HW assignment.]
When running a t-test, we can account for heterogeneity in our variance by using Welch’s t-test, which does not have the same assumption about variance as Student’s t-test (the general default type of t-test). R defaults to using Welch’s t-test so this doesn’t require any changes on our part! Even if your data has no issues with homogeneity of variance, you’ll still use Welch’s t-test – it handles the potential issues around variance well and there are no real downsides. We’re just using Levene’s test here to get into the habit of checking the homogeneity of our variance, even if we already have the solution for any potential problems.
My independent variable has more than two levels. To proceed with this analysis, I will drop the isolated dwellings and village participants from my sample. I will make a note to discuss this issue in my Method write-up and in my Discussion as a limitation of my study.
My data also has no issues regarding homogeneity but going to use Welch’s t-test anyway.
[Revise the above statements for you HW assignment.]
# very simple! we use the same formula of y~x, where y is our DV and x is our IV
t_output <- t.test(d$pas_covid~d$urban_rural)
t_output
##
## Welch Two Sample t-test
##
## data: d$pas_covid by d$urban_rural
## t = -0.035253, df = 116.02, p-value = 0.9719
## alternative hypothesis: true difference in means between group city and group town is not equal to 0
## 95 percent confidence interval:
## -0.1974991 0.1905915
## sample estimates:
## mean in group city mean in group town
## 3.170370 3.173824
# once again, we use the same formula, y~x, to calculate cohen's d
d_output <- cohen.d(d$pas_covid~d$urban_rural)
d_output
##
## Cohen's d
##
## d estimate: -0.005071228 (negligible)
## 95 percent confidence interval:
## lower upper
## -0.3026600 0.2925175
To test our hypothesis that there will be a significant difference in anxiety by city vs town, we used an independent samples t-test. This required us to drop our isolated dwellings and village participants from our sample, as we are limited to a two-group comparison when using this test. We tested the homogeneity of variance with Levene’s test and found signs of homogeneity (p = 0.18). Our data met all assumptions of a t-test.
We found that there was not a significant different in anxiety between city and town. City (M = 3.17, SD = 0.63) was not significantly different from town(M = 3.17, SD = 0.7); t(116.02) = -0.035, p = 0.97 (see Figure 1). The effect size was calculated using Cohen’s d, with a value of -0.005 (negligible effect; Cohen, 1988). Hypothesis not supported, groups are not significant. [Revise the above statements for you HW assignment.]
References
Cohen J. (1988). Statistical Power Analysis for the Behavioral Sciences. New York, NY: Routledge Academic.