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 drop variable use NULL: let(mtcars, am = NULL) %>% head()
## 
## 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"     "sibling"    "mindful"    "efficacy"  
## [6] "support"    "stress"
head(df)
##          ResponseId gender              sibling mindful efficacy  support
## 1 R_BJN3bQqi1zUMid3      f at least one sibling     2.4      3.4 6.000000
## 2 R_2TGbiBXmAtxywsD      m at least one sibling     1.8      3.4 6.750000
## 3 R_12G7bIqN2wB2N65      m at least one sibling     2.2      2.2 5.166667
## 4 R_39pldNoon8CePfP      f at least one sibling     2.2      2.8 5.583333
## 5 R_1QiKb2LdJo1Bhvv      m at least one sibling     3.2      3.0 6.000000
## 6 R_pmwDTZyCyCycXwB      f at least one sibling     3.4      2.4 4.500000
##   stress
## 1    3.3
## 2    3.3
## 3    4.0
## 4    3.2
## 5    3.1
## 6    3.5
str(df)
## 'data.frame':    3161 obs. of  7 variables:
##  $ ResponseId: chr  "R_BJN3bQqi1zUMid3" "R_2TGbiBXmAtxywsD" "R_12G7bIqN2wB2N65" "R_39pldNoon8CePfP" ...
##  $ gender    : chr  "f" "m" "m" "f" ...
##  $ sibling   : chr  "at least one sibling" "at least one sibling" "at least one sibling" "at least one sibling" ...
##  $ mindful   : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ 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 ...
##  $ 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, sibling, mindful, efficacy, support, stress))
# this is where I will list my variables for the homework, with stress, mindfulness, and belonging

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$sibling <- as.factor(d$sibling)

str(d)
## 'data.frame':    3161 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 ...
##  $ sibling   : Factor w/ 2 levels "at least one sibling",..: 1 1 1 1 1 1 1 2 1 1 ...
##  $ mindful   : num  2.4 1.8 2.2 2.2 3.2 ...
##  $ 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 ...
##  $ stress    : num  3.3 3.3 4 3.2 3.1 3.5 3.3 2.4 2.9 2.7 ...

Missing Data

We looked at the missing data in our dataset, and found that about 0.7% 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 vulnerable groups or skewing results. However, we will proceed for the sake of this class using the reduced dataset.

# use the gg_miss_upset() command for a visualization of your missing data

# 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)
3161/3182
## [1] 0.9934004

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_data_final.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 
## 2318  790   53
table(d2$sibling)
## 
## at least one sibling           only child 
##                 2858                  303
hist(d2$mindful)

hist(d2$efficacy)

hist(d2$support)

hist(d2$stress)

Univariate Normality

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

describe(d2)
##             vars    n    mean     sd  median trimmed     mad  min    max
## ResponseId*    1 3161 1581.00 912.65 1581.00 1581.00 1171.25 1.00 3161.0
## gender*        2 3161    1.28   0.49    1.00    1.21    0.00 1.00    3.0
## sibling*       3 3161    1.10   0.29    1.00    1.00    0.00 1.00    2.0
## mindful        4 3161    3.71   0.84    3.73    3.71    0.79 1.13    6.0
## efficacy       5 3161    3.13   0.45    3.10    3.13    0.44 1.10    4.0
## support        6 3161    5.53   1.13    5.75    5.65    0.99 0.00    7.0
## stress         7 3161    3.05   0.60    3.00    3.05    0.59 1.30    4.7
##               range  skew kurtosis    se
## ResponseId* 3160.00  0.00    -1.20 16.23
## gender*        2.00  1.39     0.86  0.01
## sibling*       1.00  2.74     5.53  0.01
## mindful        4.87 -0.06    -0.14  0.01
## efficacy       2.90 -0.26     0.53  0.01
## support        7.00 -1.11     1.49  0.02
## stress         3.40  0.04    -0.17  0.01

Bivariate Plots

Crosstabs

categorical varaibles

cross_cases(d2, gender, sibling)
 sibling 
 at least one sibling   only child 
 gender 
   f  2102 216
   m  708 82
   nb  48 5
   #Total cases  2858 303

Scatterplots

only have to do two scatterplots

plot(d2$mindful, d2$efficacy,
     main="Scatterplot of mindful and efficacy",
     xlab = "mindful",
     ylab = "efficacy")

plot(d2$support, d2$stress,
     main="Scatterplot of support and stress",
     xlab = "support",
     ylab = "stress")

Boxplots

continuous~categorical

boxplot(data=d2, stress~sibling,
        main="Boxplot of sibling and stress",
        xlab = "sibling",
        ylab = "stress")

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