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("")

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$age)
## 
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
##                1987                 113                  38                  18
table(d2$usdream)
## 
##               american dream is important and achievable for me 
##                                                            1005 
##     american dream is important but maybe not achievable for me 
##                                                             226 
## american dream is not important and maybe not achievable for me 
##                                                             411 
##        american dream is not important but is achievable for me 
##                                                             116 
##                            not sure if american dream important 
##                                                             398
# use histograms to visualize 4 continuous variables
hist(d2$belong)

hist(d2$efficacy)

hist(d2$support)

hist(d2$swb)

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 2156 1078.50 622.53 1078.50 1078.50 799.12 1.0 2156 2155.0
## age*           2 2156    1.11   0.43    1.00    1.00   0.00 1.0    4    3.0
## usdream*       3 2156    2.39   1.54    2.00    2.23   1.48 1.0    5    4.0
## swb            4 2156    4.43   1.33    4.50    4.49   1.48 1.0    7    6.0
## belong         5 2156    3.21   0.61    3.20    3.23   0.59 1.3    5    3.7
## efficacy       6 2156    3.11   0.45    3.10    3.12   0.44 1.2    4    2.8
## support        7 2156    5.53   1.14    5.75    5.65   0.99 0.0    7    7.0
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 13.41
## age*         4.44    21.33  0.01
## usdream*     0.63    -1.11  0.03
## swb         -0.35    -0.50  0.03
## belong      -0.27    -0.10  0.01
## efficacy    -0.19     0.35  0.01
## support     -1.08     1.31  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 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$belong)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$belong
## W = 0.99223, p-value = 0.000000002628
shapiro.test(d2$efficacy)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$efficacy
## W = 0.98089, p-value < 0.00000000000000022
shapiro.test(d2$support)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$support
## W = 0.92376, p-value < 0.00000000000000022
shapiro.test(d2$swb)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$swb
## W = 0.98088, p-value < 0.00000000000000022

We analyzed the skew and kurtosis of our four continuous variables and all were within the acceptable range (-2/+2).

Bivariate Plots

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$support, d2$swb,
     main="Scatterplot of Perceived Social Support and Subjective Wellbeing",
     xlab = "Perceived Social Support",
     ylab = "Subjective Wellbeing")

plot(d2$belong, d2$efficacy,
     main="Scatterplot of Need to Belong and Self-Efficacy",
     xlab = "Need to Belong",
     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, support~age,
        main="Boxplot of Perceived Social Support by Age",
        xlab = "Age",
        ylab = "Perceived Social Support")

boxplot(data=d2, efficacy~usdream,
        main="Boxplot of Self-Efficacy by Attainability of American Dream",
        xlab = "Attainability of American Dream",
        ylab = "Self-Efficacy")

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