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 rows from data: rows(mtcars, am==0)
## 
## 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/eammi2_data_final.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] "ResponseId"       "gender"           "race_rc"          "age"             
##  [5] "income"           "edu"              "sibling"          "party_rc"        
##  [9] "disability"       "marriage5"        "phys_sym"         "pipwd"           
## [13] "moa_independence" "moa_role"         "moa_safety"       "moa_maturity"    
## [17] "idea"             "swb"              "mindful"          "belong"          
## [21] "efficacy"         "support"          "socmeduse"        "usdream"         
## [25] "npi"              "exploit"          "stress"
head(df)
##          ResponseId gender race_rc                 age         income
## 1 R_BJN3bQqi1zUMid3      f   white 1 between 18 and 25          1 low
## 2 R_2TGbiBXmAtxywsD      m   white 1 between 18 and 25          1 low
## 3 R_12G7bIqN2wB2N65      m   white 1 between 18 and 25 rather not say
## 4 R_39pldNoon8CePfP      f   other 1 between 18 and 25 rather not say
## 5 R_1QiKb2LdJo1Bhvv      m   white 1 between 18 and 25       2 middle
## 6 R_pmwDTZyCyCycXwB      f   white 1 between 18 and 25 rather not say
##                            edu              sibling    party_rc  disability
## 1       2 Currently in college at least one sibling    democrat        <NA>
## 2 5 Completed Bachelors Degree at least one sibling independent        <NA>
## 3       2 Currently in college at least one sibling  apolitical psychiatric
## 4       2 Currently in college at least one sibling  apolitical        <NA>
## 5       2 Currently in college at least one sibling  apolitical        <NA>
## 6       2 Currently in college at least one sibling  apolitical        <NA>
##                                 marriage5                phys_sym    pipwd
## 1 are currently divorced from one another high number of symptoms       NA
## 2    are currently married to one another high number of symptoms       NA
## 3    are currently married to one another high number of symptoms 2.333333
## 4    are currently married to one another high number of symptoms       NA
## 5    are currently married to one another  low number of symptoms       NA
## 6    are currently married to one another high number of symptoms       NA
##   moa_independence moa_role moa_safety moa_maturity  idea      swb mindful
## 1         3.666667 3.000000       2.75     3.666667 3.750 4.333333     2.4
## 2         3.666667 2.666667       3.25     3.333333 3.875 4.166667     1.8
## 3         3.500000 2.500000       3.00     3.666667 3.750 1.833333     2.2
## 4         3.000000 2.000000       1.25     3.000000 3.750 5.166667     2.2
## 5         3.833333 2.666667       2.25     3.666667 3.500 3.666667     3.2
## 6         3.500000 3.333333       2.50     4.000000 3.250 4.000000     3.4
##   belong efficacy  support socmeduse
## 1    2.8      3.4 6.000000        47
## 2    4.2      3.4 6.750000        23
## 3    3.6      2.2 5.166667        34
## 4    4.0      2.8 5.583333        35
## 5    3.4      3.0 6.000000        37
## 6    4.2      2.4 4.500000        13
##                                                           usdream        npi
## 1               american dream is important and achievable for me 0.69230769
## 2               american dream is important and achievable for me 0.15384615
## 3 american dream is not important and maybe not achievable for me 0.07692308
## 4 american dream is not important and maybe not achievable for me 0.07692308
## 5                            not sure if american dream important 0.76923077
## 6 american dream is not important and maybe not achievable for me 0.23076923
##    exploit stress
## 1 2.000000    3.3
## 2 3.666667    3.3
## 3 4.333333    4.0
## 4 1.666667    3.2
## 5 4.000000    3.1
## 6 1.333333    3.5
str(df)
## 'data.frame':    3182 obs. of  27 variables:
##  $ ResponseId      : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender          : chr  "f" "m" "m" "f" ...
##  $ race_rc         : chr  "white" "white" "white" "other" ...
##  $ age             : chr  "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" "1 between 18 and 25" ...
##  $ income          : chr  "1 low" "1 low" "rather not say" "rather not say" ...
##  $ edu             : chr  "2 Currently in college" "5 Completed Bachelors Degree" "2 Currently in college" "2 Currently in college" ...
##  $ sibling         : chr  "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
##  $ party_rc        : chr  "democrat" "independent" "apolitical" "apolitical" ...
##  $ disability      : chr  NA NA "psychiatric" NA ...
##  $ marriage5       : chr  "are currently divorced from one another" "are currently married to one another" "are currently married to one another" "are currently married to one another" ...
##  $ phys_sym        : chr  "high number of symptoms" "high number of symptoms" "high number of symptoms" "high number of symptoms" ...
##  $ pipwd           : num  NA NA 2.33 NA NA ...
##  $ moa_independence: num  3.67 3.67 3.5 3 3.83 ...
##  $ moa_role        : num  3 2.67 2.5 2 2.67 ...
##  $ moa_safety      : num  2.75 3.25 3 1.25 2.25 2.5 4 3.25 2.75 3.5 ...
##  $ moa_maturity    : num  3.67 3.33 3.67 3 3.67 ...
##  $ idea            : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ swb             : num  4.33 4.17 1.83 5.17 3.67 ...
##  $ mindful         : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ belong          : num  2.8 4.2 3.6 4 3.4 4.2 3.9 3.6 2.9 2.5 ...
##  $ efficacy        : num  3.4 3.4 2.2 2.8 3 2.4 2.3 3 3 3.7 ...
##  $ support         : num  6 6.75 5.17 5.58 6 ...
##  $ socmeduse       : int  47 23 34 35 37 13 37 43 37 29 ...
##  $ usdream         : chr  "american dream is important and achievable for me" "american dream is important and achievable for me" "american dream is not important and maybe not achievable for me" "american dream is not important and maybe not achievable for me" ...
##  $ npi             : num  0.6923 0.1538 0.0769 0.0769 0.7692 ...
##  $ exploit         : num  2 3.67 4.33 1.67 4 ...
##  $ stress          : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...

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(ResponseId, gender, race_rc, moa_maturity, idea, support, socmeduse))
# note: add variables here on line 47, can be entered without quotes in the select=c()

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$race_rc <- as.factor(d$race_rc)
str(d)
## 'data.frame':    3182 obs. of  7 variables:
##  $ ResponseId  : chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender      : Factor w/ 3 levels "f","m","nb": 1 2 2 1 2 1 1 1 1 1 ...
##  $ race_rc     : Factor w/ 7 levels "asian","black",..: 7 7 7 6 7 7 7 7 4 4 ...
##  $ moa_maturity: num  3.67 3.33 3.67 3 3.67 ...
##  $ idea        : num  3.75 3.88 3.75 3.75 3.5 ...
##  $ support     : num  6 6.75 5.17 5.58 6 ...
##  $ socmeduse   : int  47 23 34 35 37 13 37 43 37 29 ...

Missing Data

About 2% of the participants in our sample skipped at least one item. These participants were dropped from our analysis. This does not raise any major concerns as it is under the 5% missing data rule of thumb. The total sample size without the missing data is 3130.

# 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)
3130/3182
## [1] 0.9836581

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/eammi2_hw.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)
## 
##    f    m   nb 
## 2296  781   53
table(d2$race_rc)
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##         206         241         279         288          10          96 
##       white 
##        2010
hist(d2$moa_maturity)

hist(d2$idea)

hist(d2$support)

hist(d2$socmeduse)

Univariate Normality

We analyzed the skew and kurtosis of our continuous variables and most of them were within the accepted range (-2/+2) for univariate normality. However, one of them (idea or inventory of the dimensions of emerging adulthood) were outside of the accepted range.

describe(d2)
##              vars    n    mean     sd  median trimmed     mad min  max range
## ResponseId*     1 3130 1565.50 903.70 1565.50 1565.50 1160.13   1 3130  3129
## gender*         2 3130    1.28   0.49    1.00    1.21    0.00   1    3     2
## race_rc*        3 3130    5.55   2.12    7.00    5.90    0.00   1    7     6
## moa_maturity    4 3130    3.59   0.43    3.67    3.65    0.49   1    4     3
## idea            5 3130    3.58   0.38    3.62    3.62    0.37   1    4     3
## support         6 3130    5.54   1.13    5.75    5.66    0.99   0    7     7
## socmeduse       7 3130   34.49   8.57   35.00   34.75    7.41  11   55    44
##               skew kurtosis    se
## ResponseId*   0.00    -1.20 16.15
## gender*       1.39     0.87  0.01
## race_rc*     -1.00    -0.64  0.04
## moa_maturity -1.20     1.87  0.01
## idea         -1.49     4.22  0.01
## support      -1.10     1.41  0.02
## socmeduse    -0.30     0.26  0.15

Bivariate Plots

Crosstabs

cross_cases(d2, gender, support)
 support 
 0   1   1.08333333333333   1.16666666666667   1.25   1.33333333333333   1.41666666666667   1.5   1.58333333333333   1.66666666666667   1.75   1.83333333333333   1.91666666666667   2   2.08333333333333   2.16666666666667   2.25   2.33333333333333   2.41666666666667   2.5   2.58333333333333   2.66666666666667   2.75   2.83333333333333   2.91666666666667   3   3.08333333333333   3.16666666666667   3.25   3.33333333333333   3.41666666666667   3.5   3.58333333333333   3.66666666666667   3.75   3.83333333333333   3.91666666666667   4   4.08333333333333   4.16666666666667   4.25   4.33333333333333   4.41666666666667   4.5   4.58333333333333   4.66666666666667   4.75   4.83333333333333   4.91666666666667   5   5.08333333333333   5.16666666666667   5.25   5.33333333333333   5.41666666666667   5.5   5.58333333333333   5.66666666666667   5.75   5.83333333333333   5.91666666666667   6   6.08333333333333   6.16666666666667   6.25   6.33333333333333   6.41666666666667   6.5   6.58333333333333   6.66666666666667   6.75   6.83333333333333   6.91666666666667   7 
 gender 
   f  1 5 1 2 1 1 2 1 2 1 1 3 2 4 2 1 4 1 6 5 5 6 5 4 8 9 13 12 6 10 6 7 9 12 16 11 32 8 19 32 23 28 33 41 32 40 38 44 45 56 47 46 54 55 65 60 55 75 77 73 133 79 78 68 71 73 75 65 62 56 48 52 173
   m  2 2 1 1 1 3 1 1 3 1 3 1 1 1 1 3 3 1 1 2 6 9 6 9 5 7 9 9 10 7 7 11 9 15 8 13 8 16 14 18 23 9 25 30 10 16 22 33 23 24 25 23 35 21 27 18 23 26 12 19 22 20 13 6 47
   nb  1 1 1 1 1 1 1 1 1 2 3 1 1 2 1 3 1 2 1 2 3 4 2 2 3 2 2 1 1 2 1 1 1
   #Total cases  1 7 3 2 1 1 2 2 2 2 1 5 5 5 3 3 2 7 1 8 6 5 7 7 7 11 10 14 15 12 19 12 17 14 20 26 21 43 17 29 44 33 43 41 54 42 57 55 63 68 67 72 77 64 73 90 93 82 101 102 98 171 102 105 88 95 100 89 85 84 76 62 58 221
cross_cases(d2, race_rc, support)
 support 
 0   1   1.08333333333333   1.16666666666667   1.25   1.33333333333333   1.41666666666667   1.5   1.58333333333333   1.66666666666667   1.75   1.83333333333333   1.91666666666667   2   2.08333333333333   2.16666666666667   2.25   2.33333333333333   2.41666666666667   2.5   2.58333333333333   2.66666666666667   2.75   2.83333333333333   2.91666666666667   3   3.08333333333333   3.16666666666667   3.25   3.33333333333333   3.41666666666667   3.5   3.58333333333333   3.66666666666667   3.75   3.83333333333333   3.91666666666667   4   4.08333333333333   4.16666666666667   4.25   4.33333333333333   4.41666666666667   4.5   4.58333333333333   4.66666666666667   4.75   4.83333333333333   4.91666666666667   5   5.08333333333333   5.16666666666667   5.25   5.33333333333333   5.41666666666667   5.5   5.58333333333333   5.66666666666667   5.75   5.83333333333333   5.91666666666667   6   6.08333333333333   6.16666666666667   6.25   6.33333333333333   6.41666666666667   6.5   6.58333333333333   6.66666666666667   6.75   6.83333333333333   6.91666666666667   7 
 race_rc 
   asian  1 1 1 1 1 1 1 1 2 1 2 1 1 3 1 2 1 2 1 1 3 2 3 3 4 3 4 4 7 2 4 5 2 5 6 6 5 8 4 5 4 8 5 10 10 4 5 3 4 6 9 2 10 4 2 9
   black  1 1 2 2 1 1 2 1 1 2 2 3 2 5 4 3 3 5 3 3 6 2 1 2 2 6 3 8 1 7 1 7 6 6 5 8 6 2 6 4 8 7 9 2 13 7 8 5 8 7 3 4 1 2 3 18
   hispanic  1 1 1 1 1 1 1 1 2 1 1 1 1 2 2 3 2 3 1 1 7 2 9 6 3 3 6 4 4 4 6 8 7 11 11 7 6 9 8 4 8 7 8 12 6 5 5 4 10 12 7 5 7 8 5 17
   multiracial  1 1 1 1 1 2 1 1 2 1 1 3 2 2 1 2 1 2 2 4 2 1 2 1 5 5 6 5 4 5 5 9 10 5 9 5 5 7 6 7 7 9 9 10 7 17 7 10 11 7 8 7 9 5 5 3 1 20
   nativeamer  1 1 1 1 1 1 1 1 2
   other  1 1 1 1 2 2 2 2 2 3 1 2 4 1 2 2 1 1 1 2 2 2 4 3 3 7 8 2 7 1 3 2 1 2 1 1 1 4 2 1 5
   white  5 1 2 1 1 1 2 1 3 1 2 1 1 5 4 3 3 2 4 3 5 5 8 4 4 10 8 13 6 7 16 12 24 9 20 28 15 21 26 30 24 33 37 34 43 42 45 45 36 52 55 67 53 66 60 74 111 70 75 60 79 67 56 55 67 49 42 46 150
   #Total cases  1 7 3 2 1 1 2 2 2 2 1 5 5 5 3 3 2 7 1 8 6 5 7 7 7 11 10 14 15 12 19 12 17 14 20 26 21 43 17 29 44 33 43 41 54 42 57 55 63 68 67 72 77 64 73 90 93 82 101 102 98 171 102 105 88 95 100 89 85 84 76 62 58 221

Scatterplots

plot(d2$race_rc, d2$moa_maturity,
     main="Scatterplot of race_rc and moa_maturity",
     xlab = "race_rc",
     ylab = "moa_maturity")

plot(d2$race_rc, d2$idea,
     main="Scatterplot of race_rc and idea",
     xlab = "race_rc",
     ylab = "idea")

Boxplots

boxplot(data=d2,socmeduse~gender,
        main="Boxplot of gender and socmeduse",
        xlab = "gender",
        ylab = "socmeduse")

boxplot(data=d2,socmeduse~race_rc,
        main="Boxplot of race_rc and socmeduse",
        xlab = "race_rc",
        ylab = "socmeduse")