Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# after download is finished, insert a "#" before the install function so that the file will Knit later
# then run the library function calling that package

#install.packages("psych")
#install.packages("expss")

library(psych) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To aggregate data: take(mtcars, mean_mpg = mean(mpg), by = am)
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

Import & Examine Data

# Import the "projectdata.csv" file

d2 <- read.csv("Data/projectdata.csv")

str(d2)
## 'data.frame':    687 obs. of  7 variables:
##  $ X          : int  520 2814 3146 3295 717 6056 4753 5365 1965 1246 ...
##  $ education  : chr  "1 equivalent to not completing high school" "prefer not to say" "2 equivalent to high school completion" "prefer not to say" ...
##  $ ethnicity  : chr  "Prefer not to say" "White - British, Irish, other" "Asian/Asian British - Indian, Pakistani, Bangladeshi, other" "Asian/Asian British - Indian, Pakistani, Bangladeshi, other" ...
##  $ big5_open  : num  3.67 4.33 5.67 6 5.67 ...
##  $ mfq_state  : num  3 4.38 4.88 4.88 4.88 ...
##  $ gad        : num  1.14 1.29 1 1 1.14 ...
##  $ isolation_c: num  1 1 1 1 1 1 1 1 1 1 ...
# Note: for the HW, you will import "projectdata.csv" that you created and exported in the Data Prep Lab

Univariate Plots: Histograms & Tables

Tables are used to visualize individual categorical variables. Histograms are used to visualize individual continuous variables.

# use tables to visualize categorical data (2 variables)
table(d2$education)
## 
##              1 equivalent to not completing high school 
##                                                     276 
##                  2 equivalent to high school completion 
##                                                     259 
## 3 equivalent to vocational/technical program completion 
##                                                       3 
##                        4 equivalent to AP/IB completion 
##                                                      91 
##                                  5 undergraduate degree 
##                                                       1 
##                                       prefer not to say 
##                                                      57
table(d2$ethnicity)
## 
##  Asian/Asian British - Indian, Pakistani, Bangladeshi, other 
##                                                           91 
##              Black/Black British - Caribbean, African, other 
##                                                           16 
##                                      Chinese/Chinese British 
##                                                            8 
## Middle Eastern/Middle Eastern British - Arab, Turkish, other 
##                                                            9 
##                                           Mixed race - other 
##                                                           30 
##                   Mixed race - White and Black/Black British 
##                                                           13 
##                                           Other ethnic group 
##                                                            6 
##                                            Prefer not to say 
##                                                           14 
##                                White - British, Irish, other 
##                                                          500
# use histograms to visualize continuous data (4 variables)
hist(d2$big5_open)

hist(d2$mfq_state)

hist(d2$gad)

hist(d2$isolation_c)

Univariate Normality for Continuous Variables

describe(d2)
##             vars   n    mean      sd  median trimmed     mad min    max  range
## X              1 687 5181.60 2579.11 5747.00 5327.58 3024.50  20 8860.0 8840.0
## education*     2 687    2.20    1.50    2.00    1.92    1.48   1    6.0    5.0
## ethnicity*     3 687    7.37    2.94    9.00    7.96    0.00   1    9.0    8.0
## big5_open      4 687    5.25    1.13    5.33    5.35    0.99   1    7.0    6.0
## mfq_state      5 687    3.97    0.98    4.00    4.00    0.93   1    6.0    5.0
## gad            6 687    2.19    0.93    2.00    2.13    1.06   1    4.0    3.0
## isolation_c    7 687    2.28    0.81    2.25    2.29    1.11   1    3.5    2.5
##              skew kurtosis    se
## X           -0.41    -1.11 98.40
## education*   1.38     0.90  0.06
## ethnicity*  -1.45     0.34  0.11
## big5_open   -0.82     0.72  0.04
## mfq_state   -0.34    -0.06  0.04
## gad          0.44    -1.03  0.04
## isolation_c -0.01    -1.22  0.03
## For the required write-up below, choose one of these options to paste and edit below based on your output.

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

## OPTION 2
# We analyzed the skew and kurtosis of our continuous variables and (#) were within the accepted range (-2/+2). However, (#) variables (list variable name(s) here) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

Write-up of Normality

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

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, education, 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 
 education 
   1 equivalent to not completing high school  32 6 3 4 17 7 3 6 198
   2 equivalent to high school completion  36 7 2 4 10 5 2 4 189
   3 equivalent to vocational/technical program completion  1 2
   4 equivalent to AP/IB completion  9 3 1 2 76
   5 undergraduate degree  1
   prefer not to say  14 1 1 1 1 4 35
   #Total cases  91 16 8 9 30 13 6 14 500
## Some students may have issues with this function working. If this happens to you, please try these 2 options:
## Option 1: install the "maditr" package and then call in its library.
## Option 2: If Option 1 doesn't work, then you will use xtabs() instead. Fill in the code below and remove the "#" to run. Then hashtag out the cross_cases() line.

# xtabs(~ + , data=)

# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$big5_open, d2$mfq_state,
     main="Scatterplot of Openess and Mental Flexibility State",
     xlab = "Openness",
     ylab = "Mental Flexibility State")

plot(d2$gad, d2$isolation_c,
     main="Scatterplot of General Anxiety Disorder and Isolation ",
     xlab = "General Anxiety Disorder",
     ylab = "Isolation")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your potential hypotheses. You may repeat 1 variable to see its association with 2 others. You will need replace the variable names on the first line of the function as well as the 'main' (aka plot title), 'xlab' and 'ylab' lines to correctly label the graphs -- remember to use the actual construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.

Boxplots

Boxplots are used to visualize combinations of one categorical and one continuous variable.

# ORDER MATTERS HERE: 'continuous variable' ~ 'categorical variable' 

boxplot(data=d2, big5_open~education,
        main="Boxplot of Openness by Education ",
        xlab = "Education",
        ylab = "Openness")

boxplot(data=d2, gad~ethnicity,
        main="Boxplot of General Anxiety Disorder by Ethnicity",
        xlab = "Ethnicity",
        ylab = "General Anxiety Disorder")

# Note: for HW, you will choose to plot 2 combos of any of your 4 continuous variables with either of your 2 categorical variables, based on your potential hypotheses. You may repeat 1 variable to see its association with others. Again, will need replace the variable names on the first line of the function as well as the 'main' (aka plot title), 'xlab' and 'ylab' lines to correctly label the graphs -- remember to use the actual construct names, NOT their R abbrev or full scales, so someone reading your plots can understand them.

That’s it!!