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$phys_sym)
## 
##   high number of symptoms    low number of symptoms medium number of symptoms 
##                       856                       585                      1678
table(d2$usdream)
## 
##               american dream is important and achievable for me 
##                                                            1439 
##     american dream is important but maybe not achievable for me 
##                                                             343 
## american dream is not important and maybe not achievable for me 
##                                                             581 
##        american dream is not important but is achievable for me 
##                                                             181 
##                            not sure if american dream important 
##                                                             575
# use histograms to visualize 4 continuous variables
hist(d2$moa_maturity)

hist(d2$idea)

hist(d2$mindful)

hist(d2$stress)

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
## ResponseID*     1 3119 1560.00 900.52 1560.00 1560.00 1156.43 1.00 3119.0
## phys_sym*       2 3119    2.26   0.86    3.00    2.33    0.00 1.00    3.0
## usdream*        3 3119    2.39   1.54    2.00    2.24    1.48 1.00    5.0
## moa_maturity    4 3119    3.59   0.43    3.67    3.65    0.49 1.00    4.0
## idea            5 3119    3.57   0.38    3.62    3.62    0.37 1.00    4.0
## mindful         6 3119    3.71   0.84    3.73    3.71    0.79 1.13    6.0
## stress          7 3119    3.05   0.60    3.00    3.05    0.59 1.30    4.7
##                range  skew kurtosis    se
## ResponseID*  3118.00  0.00    -1.20 16.12
## phys_sym*       2.00 -0.53    -1.45  0.02
## usdream*        4.00  0.62    -1.13  0.03
## moa_maturity    3.00 -1.20     1.89  0.01
## idea            3.00 -1.49     4.20  0.01
## mindful         4.87 -0.06    -0.12  0.02
## stress          3.40  0.04    -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 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_maturity)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$moa_maturity
## W = 0.83584, p-value < 0.00000000000000022
shapiro.test(d2$idea)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$idea
## W = 0.8813, p-value < 0.00000000000000022
shapiro.test(d2$mindful)               
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$mindful
## W = 0.99794, p-value = 0.0004142
shapiro.test(d2$stress)               
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$stress
## W = 0.99614, p-value = 0.0000003338

We analyzed the skew and kurtosis of our four continuous variables and three were within the acceptable range (-2/+2). However, one variable, the Inventory of the Dimensions of Emerging Adulthood, was outside of the acceptable range (4.20). 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$mindful, d2$stress,
     main="Scatterplot of Mindfulness and Stress",
     xlab = "Mindfulness",
     ylab = "Stress")

plot(d2$moa_maturity, d2$idea,
     main="Scatterplot of Maturity Importance and Emerging Adulthood",
     xlab = "Maturity Importance",
     ylab = "Emerging Adulthood")

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

boxplot(data=d2, moa_maturity~usdream,
        main="Boxplot of Maturity by American Dream Attainability ",
        xlab = "American Dream Attainability",
        ylab = "Maturity")

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