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 several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)

Import & Examine Data

# Import the "projectdata.csv" file

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

str(d2)
## 'data.frame':    599 obs. of  7 variables:
##  $ X          : int  2814 3295 717 6056 4753 5365 2044 1246 1250 1761 ...
##  $ urban_rural: chr  "town" "town" "village" "city" ...
##  $ treatment  : chr  "not in treatment" "no psychological disorders" "not in treatment" "not in treatment" ...
##  $ mfq_state  : num  4.38 4.88 4.88 3.75 5.88 ...
##  $ phq        : num  1.44 1.33 1.44 1 1.33 ...
##  $ support    : num  3 4 3.67 3.67 5 ...
##  $ rse        : num  3.1 3 3 3 4 3.8 2.5 3.8 3.7 3.2 ...
# 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$urban_rural)
## 
##              city isolated dwelling              town           village 
##               141                14               267               177
table(d2$treatment)
## 
##                    in treatment      no psychological disorders 
##                              45                             230 
##                not in treatment                           other 
##                             264                              11 
##               seeking treatment treatment disrupted by COVID-19 
##                              20                              29
# use histograms to visualize continuous data (4 variables)
hist(d2$mfq_state)

hist(d2$phq)

hist(d2$support)

hist(d2$rse)

Univariate Normality for Continuous Variables

describe(d2)
##              vars   n    mean      sd  median trimmed     mad min  max range
## X               1 599 5161.88 2581.36 5745.00 5309.03 3027.47  20 8834  8814
## urban_rural*    2 599    2.80    1.11    3.00    2.88    1.48   1    4     3
## treatment*      3 599    2.70    1.09    3.00    2.55    1.48   1    6     5
## mfq_state       4 599    3.95    0.98    4.00    3.98    0.93   1    6     5
## phq             5 599    2.29    0.87    2.22    2.24    0.99   1    4     3
## support         6 599    3.47    0.94    3.50    3.51    0.99   1    5     4
## rse             7 599    2.48    0.71    2.50    2.48    0.74   1    4     3
##               skew kurtosis     se
## X            -0.41    -1.11 105.47
## urban_rural* -0.65    -0.93   0.05
## treatment*    1.33     2.35   0.04
## mfq_state    -0.31    -0.10   0.04
## phq           0.36    -0.93   0.04
## support      -0.29    -0.64   0.04
## rse           0.03    -0.80   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

We analyzed the skew and kurtosis of our continuous variables and 5 were within the accepted range (-2/+2). However, 1 variables (mfq_state) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, urban_rural, treatment)
 treatment 
 in treatment   no psychological disorders   not in treatment   other   seeking treatment   treatment disrupted by COVID-19 
 urban_rural 
   city  17 53 57 2 3 9
   isolated dwelling  1 5 4 1 1 2
   town  14 96 128 5 7 17
   village  13 76 75 3 9 1
   #Total cases  45 230 264 11 20 29
## 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$mfq_state, d2$phq,
     main="Scatterplot of Mental Flexibility and Patient Health Questionnaire",
     xlab = "Mental Flexibility",
     ylab = "Patient Health Questionnaire")

plot(d2$support, d2$rse,
     main="Scatterplot of Social Support and Self-Esteem",
     xlab = "Agreeableness",
     ylab = "Extraversion")

# 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, support~urban_rural,
        main="Boxplot of Social Support by Location",
        xlab = "Social Support",
        ylab = "Location")

boxplot(data=d2, phq~treatment,
        main="Boxplot of Patient Health by Mental Health Treatment",
        xlab = "Patient Health",
        ylab = "Mental Health Treatment")

# 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!!