Basic Statistics

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# insert the "#" before the install function so that the file will Knit later
# then run the library function calling that package

#install.packages("psych")

library(psych) # for the describe() command

Import Data

# Import the "fakedata.csv" file for this Lab

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

# Note: for your Project version, you will import "projectdata.csv" that you created and exported at the end of the Project Data Prep assignment.

Univariate Plots: Histograms & Tables

Tables are used to visualize individual categorical variables. Histograms are used to visualize individual continuous variables.

# use tables to visualize 2 categorical variables
table(d2$income)
## 
##          1 low       2 middle         3 high rather not say 
##            860            866            525            839
table(d2$marriage5)
## 
##             are currently divorced from one another 
##                                                 728 
##                are currently married to one another 
##                                                2079 
##       never married each other and are not together 
##                                                 238 
## never married each other but are currently together 
##                                                  45
# use histograms to visualize 4 continuous variables
hist(d2$moa_safety)

hist(d2$support)

hist(d2$stress)

hist(d2$efficacy)

Univariate Normality for Continuous Variables (individually)

# quick & dirty normality test: check skew & kurtosis values
describe(d2)
##             vars    n    mean     sd  median trimmed     mad min    max  range
## ResponseID*    1 3090 1545.50 892.15 1545.50 1545.50 1145.31 1.0 3090.0 3089.0
## income*        2 3090    2.43   1.16    2.00    2.42    1.48 1.0    4.0    3.0
## marriage5*     3 3090    1.87   0.60    2.00    1.83    0.00 1.0    4.0    3.0
## moa_safety     4 3090    3.20   0.64    3.25    3.26    0.74 1.0    4.0    3.0
## support        5 3090    5.54   1.13    5.75    5.66    0.99 0.0    7.0    7.0
## stress         6 3090    3.05   0.60    3.00    3.05    0.59 1.3    4.7    3.4
## efficacy       7 3090    3.13   0.44    3.10    3.13    0.44 1.1    4.0    2.9
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 16.05
## income*      0.15    -1.43  0.02
## marriage5*   0.46     1.47  0.01
## moa_safety  -0.71     0.04  0.01
## support     -1.11     1.49  0.02
## stress       0.03    -0.16  0.01
## efficacy    -0.25     0.48  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 four continuous variables and all were within the acceptable range (-2/+2).

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

# Objective normality test: Shapiro-Wilks test
# We want the test to be NON-SIG (aka > .05) to indicate normality

options(scipen = 999)
# the above code turns off scientific notation


shapiro.test(d2$moa_safety)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$moa_safety
## W = 0.92921, p-value < 0.00000000000000022
shapiro.test(d2$support)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$support
## W = 0.92269, p-value < 0.00000000000000022
shapiro.test(d2$efficacy)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$efficacy
## W = 0.97875, p-value < 0.00000000000000022
shapiro.test(d2$stress)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$stress
## W = 0.99622, p-value = 0.0000004856

We analyzed the skew and kurtosis of our four continuous variables and (#) were within the acceptable range (-2/+2). However, (3) variables (moa_safety,support and efficacy) were outside of the acceptable range. For our analysis we will use them anyway, but outside of class this is bad practice.

Bivariate Plots

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$stress, d2$moa_safety,
     main="Scatterplot of Stress and Safety",
     xlab = "Stress",
     ylab = "Safety")

plot(d2$support, d2$efficacy,
     main="Scatterplot of Social Support and Self-Efficacy",
     xlab = "Social Support",
     ylab = "Self-Efficacy")

# 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~income,
        main="Boxplot of Stress by Income",
        xlab = " Income",
        ylab = "Stress")

boxplot(data=d2, support~marriage5,
        main="Boxplot of Social Support by Parent Marital status ",
        xlab = "Parent Marital status",
        ylab = "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.

Those are the basics!!