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 get total summary skip 'by' argument: take_all(mtcars, mean)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by

##Import Data

# Import the "fakedata.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$urban_rural)
## 
##              city isolated dwelling              town           village 
##                60                 8               163               125
table(d2$mhealth)
## 
##              anxiety disorder                       bipolar 
##                            30                             3 
##                    depression              eating disorders 
##                            17                             1 
##                    none or NA obsessive compulsive disorder 
##                           288                             4 
##                         other                          ptsd 
##                             7                             6
# use histograms to visualize continuous data
hist(d2$pas_covid)

hist(d2$phq)

hist(d2$isolation_a)

hist(d2$support)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##              vars   n    mean      sd  median trimmed     mad  min     max
## X               1 356 3484.61 2207.23 3433.50 3359.77 2505.59 1.00 8812.00
## urban_rural*    2 356    2.99    1.03    3.00    3.11    1.48 1.00    4.00
## mhealth*        3 356    4.64    1.32    5.00    4.89    0.00 1.00    8.00
## pas_covid       4 356    3.19    0.69    3.22    3.19    0.66 1.33    5.00
## phq             5 356    1.52    0.54    1.33    1.43    0.33 1.00    3.78
## isolation_a     6 356    1.69    0.71    1.50    1.60    0.74 1.00    3.50
## support         7 356    3.86    0.88    4.00    3.95    0.74 1.00    5.00
##                range  skew kurtosis     se
## X            8811.00  0.39    -0.49 116.98
## urban_rural*    3.00 -0.92    -0.25   0.05
## mhealth*        7.00 -1.52     3.10   0.07
## pas_covid       3.67  0.02    -0.06   0.04
## phq             2.78  1.65     2.97   0.03
## isolation_a     2.50  0.86    -0.36   0.04
## support         4.00 -0.81     0.12   0.05
## 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, urban_rural, mhealth)
 mhealth 
 anxiety disorder   bipolar   depression   eating disorders   none or NA   obsessive compulsive disorder   other   ptsd 
 urban_rural 
   city  2 4 1 50 1 1 1
   isolated dwelling  1 7
   town  23 3 9 124 2 2
   village  4 4 107 3 4 3
   #Total cases  30 3 17 1 288 4 7 6
# 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$pas_covid, d2$isolation,
     main="Scatterplot of pas_covid and isolation_a",
     xlab = "pas_covid",
     ylab = "isolation_a")

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

# 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, pas_covid~urban_rural,
        main="Boxplot of urban_rural and pas_covid",
        xlab = "urban_rural",
        ylab = "pas_covid")

boxplot(data=d2, isolation_a~urban_rural,
        main="Boxplot of urban_rural and isolation_a",
        xlab = "urban_rural",
        ylab = "isolation_a")

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