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$gender)
## 
##    f    m   nb 
## 1543  529   31
table(d2$age)
## 
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
##                1937                 112                  37                  17
# use histograms to visualize 4 continuous variables
hist(d2$moa_independence)

hist(d2$swb)

hist(d2$mindful)

hist(d2$belong)

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 2103 1052.00 607.23 1052.00 1052.00 779.85 1.00 2103
## gender*             2 2103    1.28   0.48    1.00    1.21   0.00 1.00    3
## age*                3 2103    1.11   0.43    1.00    1.00   0.00 1.00    4
## moa_independence    4 2103    3.54   0.47    3.67    3.61   0.49 1.00    4
## swb                 5 2103    4.43   1.33    4.50    4.49   1.48 1.00    7
## mindful             6 2103    3.71   0.84    3.73    3.72   0.79 1.13    6
## belong              7 2103    3.21   0.61    3.20    3.23   0.59 1.30    5
##                    range  skew kurtosis    se
## ResponseID*      2102.00  0.00    -1.20 13.24
## gender*             2.00  1.36     0.74  0.01
## age*                3.00  4.43    21.23  0.01
## moa_independence    3.00 -1.49     2.74  0.01
## swb                 6.00 -0.36    -0.49  0.03
## mindful             4.87 -0.04    -0.15  0.02
## belong              3.70 -0.27    -0.11  0.01
## For the required write-up below, choose one of these options to paste and edit below based on your output.



## OPTION 2
# We analyzed the skew and kurtosis of our four continuous variables and 3 were within the acceptable range (-2/+2). However, 1 variable (moa_independence) was outside of the acceptable range. For our analysis we will use it 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_independence)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$moa_independence
## W = 0.85047, p-value < 0.00000000000000022
shapiro.test(d2$swb)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$swb
## W = 0.98058, p-value = 0.0000000000000002522
shapiro.test(d2$mindful)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$mindful
## W = 0.99793, p-value = 0.008194
shapiro.test(d2$belong)
## 
##  Shapiro-Wilk normality test
## 
## data:  d2$belong
## W = 0.99202, p-value = 0.000000002546

We analyzed the skew and kurtosis of our four continuous variables and 3 were within the acceptable range (-2/+2). However, 1 variable (moa_independence) was outside of the acceptable range. For our analysis we will use it 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$belong,
     main="Scatterplot of Mindfulness and Belongingness",
     xlab = "Mindfulness",
     ylab = "Belongingness")

plot(d2$moa_independence, d2$swb,
     main="Scatterplot of Independence and Subjective Well-Being",
     xlab = "Independence",
     ylab = "Subjective Well-Being")

# 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, moa_independence~gender,
        main="Boxplot of Independence by Gender",
        xlab = "Gender",
        ylab = "Independence")

boxplot(data=d2, swb~age,
        main="Boxplot of Subjective Well-Being by Age ",
        xlab = "Age",
        ylab = "Subjective Well-Being")

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