Data Prep

Load Libraries

# if you haven't run this code before, you'll need to download the below packages first
# instructions on how to do this are included in the video
# but as a reminder, you use the packages tab to the right

library(psych) # for the describe() command
library(naniar) # for the gg_miss-upset() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To select columns from data: columns(mtcars, mpg, vs:carb)
## 
## Attaching package: 'expss'
## The following object is masked from 'package:naniar':
## 
##     is_na

Import Data

# for the lab, you'll import the CSV file you downloaded along with the current file we're working in (an RMD file)
# for the homework, you'll download the CSV file from your chosen README page (should be titled arc_data_final.csv or eammi2_data_final.csv)
df <- read.csv(file="data/arc_data_final-1.csv", header=T)

Viewing Data

# these are commands useful for viewing a dataframe
# you can also click the object in the environment tab to view it in a new window
names(df)
##  [1] "X"                    "gender"               "trans"               
##  [4] "sexual_orientation"   "ethnicity"            "relationship_status" 
##  [7] "age"                  "urban_rural"          "income"              
## [10] "education"            "employment"           "treatment"           
## [13] "health"               "mhealth"              "sleep_hours"         
## [16] "exercise_cat"         "pet"                  "covid_pos"           
## [19] "covid_neg"            "big5_open"            "big5_con"            
## [22] "big5_agr"             "big5_neu"             "big5_ext"            
## [25] "pswq_std"             "iou"                  "mfq_26"              
## [28] "mfq_state"            "rse"                  "school_covid_support"
## [31] "school_att"           "pas_covid"            "pss"                 
## [34] "phq"                  "gad"                  "edeq12"              
## [37] "brs"                  "swemws"               "isolation"           
## [40] "support"
head(df)
##    X gender trans    sexual_orientation                     ethnicity
## 1  1 female    no Heterosexual/Straight White - British, Irish, other
## 2 20   male    no Heterosexual/Straight White - British, Irish, other
## 3 30 female    no Heterosexual/Straight White - British, Irish, other
## 4 31 female    no Heterosexual/Straight White - British, Irish, other
## 5 32   <NA>  <NA>                  <NA>                          <NA>
## 6 33 female    no Heterosexual/Straight White - British, Irish, other
##                        relationship_status                 age urban_rural
## 1 In a relationship/married and cohabiting                <NA>        city
## 2                        Prefer not to say          1 under 18        city
## 3                        Prefer not to say          1 under 18        city
## 4 In a relationship/married and cohabiting 4 between 36 and 45        town
## 5                                     <NA>                <NA>        <NA>
## 6 In a relationship/married and cohabiting 4 between 36 and 45        city
##     income                              education               employment
## 1   3 high            6 graduate degree or higher               3 employed
## 2     <NA>                      prefer not to say 1 high school equivalent
## 3     <NA> 2 equivalent to high school completion 1 high school equivalent
## 4 2 middle                 5 undergraduate degree               3 employed
## 5     <NA>                                   <NA>                     <NA>
## 6 2 middle            6 graduate degree or higher               3 employed
##                    treatment                           health          mhealth
## 1 no psychological disorders something else or not applicable       none or NA
## 2               in treatment something else or not applicable anxiety disorder
## 3           not in treatment something else or not applicable       none or NA
## 4 no psychological disorders                   two conditions       none or NA
## 5                       <NA>                             <NA>       none or NA
## 6           not in treatment something else or not applicable       none or NA
##   sleep_hours       exercise_cat                   pet covid_pos covid_neg
## 1 3 7-8 hours 1 less than 1 hour                   cat         0         0
## 2 2 5-6 hours        2 1-2 hours                   cat         0         0
## 3 3 7-8 hours        3 2-5 hours                   dog         0         0
## 4 2 5-6 hours        2 1-2 hours               no pets         0         0
## 5        <NA>               <NA>                  <NA>         0         0
## 6 3 7-8 hours        2 1-2 hours multiple types of pet         0         0
##   big5_open big5_con big5_agr big5_neu big5_ext   pswq_std      iou mfq_26
## 1  5.333333 6.000000 4.333333 6.000000 2.000000  2.3094514 3.185185   4.20
## 2  5.333333 3.333333 4.333333 6.666667 1.666667  0.8509744 4.000000   3.35
## 3  5.000000 5.333333 6.666667 4.000000 6.000000 -1.1235082 1.592593   4.65
## 4  6.000000 5.666667 4.666667 4.000000 5.000000  1.1626810 3.370370   4.65
## 5        NA       NA       NA       NA       NA         NA       NA     NA
## 6  5.000000 6.000000 6.333333 2.666667       NA -0.3424552 1.703704   4.50
##   mfq_state rse school_covid_support school_att pas_covid  pss      phq
## 1     3.625 2.3                   NA         NA  3.222222 3.25 1.333333
## 2     3.000 1.6                   NA         NA  4.555556 3.75 3.333333
## 3     5.875 3.9                   NA         NA  3.333333 1.00 1.000000
## 4     4.000 1.7                   NA         NA  4.222222 3.25 2.333333
## 5        NA  NA                   NA         NA        NA   NA       NA
## 6     4.625 3.9                   NA         NA  3.222222 2.00 1.111111
##        gad   edeq12 brs   swemws isolation  support
## 1 1.857143 1.583333  NA 2.857143      2.25 2.500000
## 2 3.857143 1.833333  NA 2.285714      3.50 2.166667
## 3 1.142857 1.000000  NA 4.285714      1.00 5.000000
## 4 2.000000 1.666667  NA 3.285714      2.50 2.500000
## 5       NA       NA  NA       NA        NA       NA
## 6 1.428571 1.416667  NA 4.000000      1.75 3.666667
str(df)
## 'data.frame':    2073 obs. of  40 variables:
##  $ X                   : int  1 20 30 31 32 33 48 49 57 58 ...
##  $ gender              : chr  "female" "male" "female" "female" ...
##  $ trans               : chr  "no" "no" "no" "no" ...
##  $ sexual_orientation  : chr  "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" "Heterosexual/Straight" ...
##  $ ethnicity           : chr  "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" "White - British, Irish, other" ...
##  $ relationship_status : chr  "In a relationship/married and cohabiting" "Prefer not to say" "Prefer not to say" "In a relationship/married and cohabiting" ...
##  $ age                 : chr  NA "1 under 18" "1 under 18" "4 between 36 and 45" ...
##  $ urban_rural         : chr  "city" "city" "city" "town" ...
##  $ income              : chr  "3 high" NA NA "2 middle" ...
##  $ education           : chr  "6 graduate degree or higher" "prefer not to say" "2 equivalent to high school completion" "5 undergraduate degree" ...
##  $ employment          : chr  "3 employed" "1 high school equivalent" "1 high school equivalent" "3 employed" ...
##  $ treatment           : chr  "no psychological disorders" "in treatment" "not in treatment" "no psychological disorders" ...
##  $ health              : chr  "something else or not applicable" "something else or not applicable" "something else or not applicable" "two conditions" ...
##  $ mhealth             : chr  "none or NA" "anxiety disorder" "none or NA" "none or NA" ...
##  $ sleep_hours         : chr  "3 7-8 hours" "2 5-6 hours" "3 7-8 hours" "2 5-6 hours" ...
##  $ exercise_cat        : chr  "1 less than 1 hour" "2 1-2 hours" "3 2-5 hours" "2 1-2 hours" ...
##  $ pet                 : chr  "cat" "cat" "dog" "no pets" ...
##  $ covid_pos           : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ covid_neg           : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ big5_open           : num  5.33 5.33 5 6 NA ...
##  $ big5_con            : num  6 3.33 5.33 5.67 NA ...
##  $ big5_agr            : num  4.33 4.33 6.67 4.67 NA ...
##  $ big5_neu            : num  6 6.67 4 4 NA ...
##  $ big5_ext            : num  2 1.67 6 5 NA ...
##  $ pswq_std            : num  2.309 0.851 -1.124 1.163 NA ...
##  $ iou                 : num  3.19 4 1.59 3.37 NA ...
##  $ mfq_26              : num  4.2 3.35 4.65 4.65 NA 4.5 NA 4.3 5.25 4.45 ...
##  $ mfq_state           : num  3.62 3 5.88 4 NA ...
##  $ rse                 : num  2.3 1.6 3.9 1.7 NA 3.9 NA 2.4 1.8 NA ...
##  $ school_covid_support: num  NA NA NA NA NA NA NA NA NA NA ...
##  $ school_att          : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ pas_covid           : num  3.22 4.56 3.33 4.22 NA ...
##  $ pss                 : num  3.25 3.75 1 3.25 NA 2 NA 2 4 1.25 ...
##  $ phq                 : num  1.33 3.33 1 2.33 NA ...
##  $ gad                 : num  1.86 3.86 1.14 2 NA ...
##  $ edeq12              : num  1.58 1.83 1 1.67 NA ...
##  $ brs                 : num  NA NA NA NA NA NA NA NA NA NA ...
##  $ swemws              : num  2.86 2.29 4.29 3.29 NA ...
##  $ isolation           : num  2.25 3.5 1 2.5 NA 1.75 NA 2 1.25 1 ...
##  $ support             : num  2.5 2.17 5 2.5 NA ...

Subsetting Data

# for the HW: use the codebook you created in the codebook activity to get the names of your variables (first column)
# enter this list of names in the select=c() argument to subset those columns from the dataframe
# variables for the lab: id, variable2, variable3, variable5, variable8, variable10, variable11
d <- subset(df, select=c(X, gender, ethnicity, gad, isolation, pss, school_covid_support))
# this is where I will list my variables! Important!

Recoding Variables

# categorical variables need to be recoded as factors
# the content of the variable will stay the same, but R will treat the variable differently at times
d$gender <- as.factor(d$gender)
d$ethnicity <- as.factor(d$ethnicity)
str(d)
## 'data.frame':    2073 obs. of  7 variables:
##  $ X                   : int  1 20 30 31 32 33 48 49 57 58 ...
##  $ gender              : Factor w/ 4 levels "female","I use another term",..: 1 3 1 1 NA 1 1 1 1 1 ...
##  $ ethnicity           : Factor w/ 9 levels "Asian/Asian British - Indian, Pakistani, Bangladeshi, other",..: 9 9 9 9 NA 9 9 9 9 9 ...
##  $ gad                 : num  1.86 3.86 1.14 2 NA ...
##  $ isolation           : num  2.25 3.5 1 2.5 NA 1.75 NA 2 1.25 1 ...
##  $ pss                 : num  3.25 3.75 1 3.25 NA 2 NA 2 4 1.25 ...
##  $ school_covid_support: num  NA NA NA NA NA NA NA NA NA NA ...

Missing Data

we looked at the missing data in the dataset, and found that about 12% of the participants in our sample skipped at least one item. We dropped these participants from our analysis, which is not advisable and runs the risk of dropping vunerable groups or skewing results. However, we will proceed for the sake of this class.

# use the gg_miss_upset() command for a visualization of your missing data
gg_miss_upset(d[-1], nsets = 6)

# use the na.omit() command to create a new dataframe in which any participants with missing data are dropped from the dataframe
d2 <- na.omit(d)
147/2073
## [1] 0.07091172

Exporting Data

# last step is to export the data after you've dropped NAs
# for the HW, the file you're exporting here is what you'll use for all future HW assignments (labs will use the files I provide you)
# make sure you give it a name that is memorable!
# and make sure you save it to your Data folder!
write.csv(d2, file="data/mydata.csv", row.names = F)

# since we've created a cleaned dataframe in d2, we'll use that for the rest of the lab/HW

Basic Statistics

Univariate Plots: Histograms & Tables

table(d2$gender)
## 
##             female I use another term               male  Prefer not to say 
##                116                  4                 25                  2
table(d2$ethnicity)
## 
##  Asian/Asian British - Indian, Pakistani, Bangladeshi, other 
##                                                           21 
##              Black/Black British - Caribbean, African, other 
##                                                            2 
##                                      Chinese/Chinese British 
##                                                            0 
## Middle Eastern/Middle Eastern British - Arab, Turkish, other 
##                                                            2 
##                                           Mixed race - other 
##                                                            5 
##                   Mixed race - White and Black/Black British 
##                                                            4 
##                                           Other ethnic group 
##                                                            1 
##                                            Prefer not to say 
##                                                            2 
##                                White - British, Irish, other 
##                                                          110
hist(d2$gad)

hist(d2$isolation)

hist(d2$pss)

hist(d2$school_covid_support)

Univariate Normality

We analyzed the skew and kurtosis of our continuous variables and all we were within the accepted range (-2/+2).

We analyzed the skew and kurtosis of our… and most were within the accepted range (-2/+2). However, some variables (list them in parentheses) were outside of accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

describe(d2)
##                      vars   n    mean     sd  median trimmed    mad  min    max
## X                       1 147 8352.36 304.22 8352.00 8352.83 385.48 7851 8867.0
## gender*                 2 147    1.41   0.82    1.00    1.25   0.00    1    4.0
## ethnicity*              3 147    7.45   2.94    9.00    8.03   0.00    1    9.0
## gad                     4 147    2.41   0.94    2.29    2.39   1.27    1    4.0
## isolation               5 147    2.39   0.76    2.50    2.41   1.11    1    3.5
## pss                     6 147    3.31   0.91    3.50    3.31   1.11    1    5.0
## school_covid_support    7 147    1.52   0.33    1.60    1.53   0.30    1    2.0
##                       range  skew kurtosis    se
## X                    1016.0 -0.02    -1.31 25.09
## gender*                 3.0  1.60     0.88  0.07
## ethnicity*              8.0 -1.53     0.58  0.24
## gad                     3.0  0.19    -1.29  0.08
## isolation               2.5 -0.16    -1.17  0.06
## pss                     4.0 -0.11    -0.66  0.07
## school_covid_support    1.0 -0.14    -1.17  0.03

Bivariate Plots

Crosstabs

cross_cases(d2, gender, ethnicity)
 ethnicity 
 Asian/Asian British - Indian, Pakistani, Bangladeshi, other   Black/Black British - Caribbean, African, other   Chinese/Chinese British   Middle Eastern/Middle Eastern British - Arab, Turkish, other   Mixed race - other   Mixed race - White and Black/Black British   Other ethnic group   Prefer not to say   White - British, Irish, other 
 gender 
   female  18 1 2 4 4 1 1 85
   I use another term  4
   male  3 1 1 20
   Prefer not to say  1 1
   #Total cases  21 2 2 5 4 1 2 110

Scatterplots

plot(d2$gad, d2$pss,
     main="Scatterplot of gad and pss",
     xlab = "gad",
     ylab = "pss")

plot(d2$isolation, d2$school_covid_support,
     main="Scatterplot of isolation and school_covid_support",
     xlab = "isolation",
     ylab = "school_covid_support")

Boxplots

boxplot(data=d2, isolation~gad,
        main="Boxplot of isolation and gad",
        xlab = "isolation",
        ylab = "gad")

boxplot(data=d2, pss~school_covid_support,
        main="Boxplot of pss and school_covid_support",
        xlab = "pss",
        ylab = "school_covid_support")