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()
## 
## Use 'expss_output_viewer()' to display tables in the RStudio Viewer.
##  To return to the console output, use 'expss_output_default()'.

##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$gender)
## 
##    f    m   nb 
## 2322  788   54
table(d2$race_rc)
## 
##       asian       black    hispanic multiracial  nativeamer       other 
##         208         247         285         293          12          97 
##       white 
##        2022
# use histograms to visualize continuous data
hist(d2$stress)

hist(d2$swb)

hist(d2$support)

hist(d2$socmeduse)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars    n    mean     sd  median trimmed     mad  min    max  range
## ResponseID*    1 3164 1582.50 913.51 1582.50 1582.50 1172.74  1.0 3164.0 3163.0
## gender*        2 3164    1.28   0.49    1.00    1.21    0.00  1.0    3.0    2.0
## race_rc*       3 3164    5.54   2.12    7.00    5.88    0.00  1.0    7.0    6.0
## swb            4 3164    4.48   1.32    4.67    4.53    1.48  1.0    7.0    6.0
## support        5 3164    5.53   1.13    5.75    5.66    0.99  0.0    7.0    7.0
## socmeduse      6 3164   34.45   8.57   35.00   34.73    7.41 11.0   55.0   44.0
## stress         7 3164    3.05   0.60    3.00    3.05    0.59  1.3    4.7    3.4
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 16.24
## gender*      1.40     0.89  0.01
## race_rc*    -0.99    -0.67  0.04
## swb         -0.36    -0.45  0.02
## support     -1.11     1.47  0.02
## socmeduse   -0.31     0.26  0.15
## stress       0.03    -0.17  0.01
## 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, gender, race_rc)
 race_rc 
 asian   black   hispanic   multiracial   nativeamer   other   white 
 gender 
   f  150 182 207 222 11 72 1478
   m  57 63 76 61 1 24 506
   nb  1 2 2 10 1 38
   #Total cases  208 247 285 293 12 97 2022
# 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$stress, d2$swb,
     main="Scatterplot of Perceived Stress and Well-Being",
     xlab = "Perceived Stress",
     ylab = "Well-Being")

plot(d2$socmeduse, d2$support,
     main="Scatterplot of Social Media Use and Perceived Social Support",
     xlab = "Social Media Use",
     ylab = "Perceived Social Support")

# 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~gender,
        main="Boxplot of Gender and Perceived Stress",
        xlab = "Gender",
        ylab = "Perceived Stress")

boxplot(data=d2, support~race_rc,
        main="Boxplot of Race/Ethnicity and Perceived Social Support",
        xlab = "Race/Ethnicity",
        ylab = "Perceived 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!!