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 modify variables or add new variables:
##              let(mtcars, new_var = 42, new_var2 = new_var*hp) %>% head()

##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$race_rc)
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##          44          58          52          89           2          22 
##       white 
##         591
table(d2$disability)
## 
## chronic health       learning          other       physical    psychiatric 
##            147            124             87             50            380 
##        sensory 
##             70
# use histograms to visualize continuous data
hist(d2$support)

hist(d2$mindful)

hist(d2$belong)

hist(d2$stress)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars   n   mean     sd median trimmed    mad  min    max  range
## ResponseID*    1 858 429.50 247.83 429.50  429.50 318.02 1.00 858.00 857.00
## race_rc*       2 858   5.77   1.99   7.00    6.15   0.00 1.00   7.00   6.00
## disability*    3 858   3.70   1.70   5.00    3.77   1.48 1.00   6.00   5.00
## support        4 858   5.32   1.23   5.58    5.45   1.11 0.00   7.00   7.00
## mindful        5 858   3.47   0.84   3.47    3.48   0.79 1.13   5.67   4.53
## belong         6 858   3.30   0.60   3.30    3.32   0.59 1.40   5.00   3.60
## stress         7 858   3.24   0.62   3.20    3.25   0.74 1.60   4.60   3.00
##              skew kurtosis   se
## ResponseID*  0.00    -1.20 8.46
## race_rc*    -1.24    -0.05 0.07
## disability* -0.44    -1.35 0.06
## support     -0.96     0.76 0.04
## mindful     -0.03    -0.19 0.03
## belong      -0.23    -0.21 0.02
## stress      -0.08    -0.45 0.02
## 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.

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,race_rc,disability)
 disability 
 chronic health   learning   other   physical   psychiatric   sensory 
 race_rc 
   asian  6 7 5 3 14 9
   black  12 10 10 7 11 8
   hispanic  10 9 3 5 22 3
   multiracial  19 11 10 3 35 11
   nativeamer  1 1
   other  4 3 5 3 6 1
   white  96 83 54 28 292 38
   #Total cases  147 124 87 50 380 70
# 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$support,d2$belong,
     main="Scatterplot of Social Support and Sense of Belonging ",
     xlab = "Social Support",
     ylab = "Sense of Belonging")

plot(d2$mindful, d2$stress,
     main="Scatterplot of Mindfullness and Percieved Stress ",
     xlab = "Mindfullness",
     ylab = "Percieved Stress")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your 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 variable names, not their 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, stress~disability,
        main="Boxplot of Disability Status and Perceieved Stress",
        xlab = "Disability Status",
        ylab = "Perceived Stress")

boxplot(data=d2, support~disability,
        main="Boxplot of Disability Status and Perceived Social Support",
        xlab = "Disability Status",
        ylab = "Percieved Social Support")

# 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 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 graphs -- remember to use the actual variable names, not their scales, so someone reading your plots can understand them.

We did it!!