Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# delete the "#" before the install function and run it to download
# re-insert the "#" 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 drop variable use NULL: let(mtcars, am = NULL) %>% head()
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

##Import Data

# Import the "projectdata.csv" file

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


# 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
table(d2$age)
## 
##          1 under 18 2 between 18 and 25 
##                 619                  50
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            75                             3 
##                    depression              eating disorders 
##                             9                            17 
##                    none or NA obsessive compulsive disorder 
##                           521                            15 
##                         other                          ptsd 
##                            17                            12
# use histograms to visualize continuous data
hist(d2$rse)

hist(d2$pas_covid)

hist(d2$phq)

hist(d2$isolation_c)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars   n    mean      sd  median trimmed     mad min    max  range
## X              1 669 5160.99 2591.41 5745.00 5302.75 3070.46  20 8860.0 8840.0
## age*           2 669    1.07    0.26    1.00    1.00    0.00   1    2.0    1.0
## mhealth*       3 669    4.61    1.43    5.00    4.85    0.00   1    8.0    7.0
## rse            4 669    2.50    0.71    2.50    2.50    0.74   1    4.0    3.0
## pas_covid      5 669    3.25    0.68    3.33    3.27    0.66   1    5.0    4.0
## phq            6 669    2.28    0.86    2.22    2.23    0.99   1    4.0    3.0
## isolation_c    7 669    2.28    0.81    2.25    2.29    1.11   1    3.5    2.5
##              skew kurtosis     se
## X           -0.40    -1.14 100.19
## age*         3.23     8.43   0.01
## mhealth*    -1.46     2.39   0.06
## rse          0.01    -0.77   0.03
## pas_covid   -0.30     0.18   0.03
## phq          0.38    -0.91   0.03
## isolation_c -0.02    -1.24   0.03
## For the required write-up below, choose one of these options to paste and edit below based on your output.

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

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

We 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,age,mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 age 
   1 under 18  66 2 9 15 487 12 16 12
   2 between 18 and 25  9 1 2 34 3 1
   #Total cases  75 3 9 17 521 15 17 12
# Note: for HW, replace the two variables with your project's categorical ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$rse, d2$isolation_c,
     main="Scatterplot of rse and isolation_c",
     xlab = "rse",
     ylab = "isolation_c")

plot(d2$pas_covid, d2$phq,
     main="Scatterplot of pas_covid and phq",
     xlab = "pas_covid",
     ylab = "phq")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your research questions/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.

Boxplots

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

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

boxplot(data=d2, rse~age,
        main="Boxplot of age and isolation_c",
        xlab = "age",
        ylab = "isolaton_c")

boxplot(data=d2, phq~mhealth,
        main="Boxplot of mhealth and phq",
        xlab = "mhealth",
        ylab = "phq")

# 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 research questions/hypotheses. You may repeat 1 variable to see its association with others. Again, 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 graph.