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)
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To select rows from data: rows(mtcars, am==0)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by

##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$income)
## 
##          1 low       2 middle         3 high rather not say 
##            866            873            529            844
table(d2$marriage5)
## 
##             are currently divorced from one another 
##                                                 731 
##                are currently married to one another 
##                                                2097 
##       never married each other and are not together 
##                                                 239 
## never married each other but are currently together 
##                                                  45
# use histograms to visualize continuous data
hist(d2$stress)

hist(d2$moa_maturity)

hist(d2$support)

hist(d2$efficacy)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##              vars    n    mean     sd  median trimmed     mad min    max  range
## ResponseId*     1 3112 1556.50 898.50 1556.50 1556.50 1153.46 1.0 3112.0 3111.0
## income*         2 3112    2.43   1.16    2.00    2.42    1.48 1.0    4.0    3.0
## marriage5*      3 3112    1.87   0.59    2.00    1.83    0.00 1.0    4.0    3.0
## efficacy        4 3112    3.13   0.44    3.10    3.13    0.44 1.1    4.0    2.9
## stress          5 3112    3.05   0.60    3.00    3.05    0.59 1.3    4.7    3.4
## moa_maturity    6 3112    3.59   0.43    3.67    3.65    0.49 1.0    4.0    3.0
## support         7 3112    5.54   1.12    5.75    5.66    0.99 0.0    7.0    7.0
##               skew kurtosis    se
## ResponseId*   0.00    -1.20 16.11
## income*       0.15    -1.43  0.02
## marriage5*    0.46     1.47  0.01
## efficacy     -0.24     0.45  0.01
## stress        0.03    -0.16  0.01
## moa_maturity -1.20     1.89  0.01
## support      -1.10     1.45  0.02
## For the required write-up below, choose one of these options to paste and edit below based on your output.

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

# We analyzed the skew and kurtosis of our continuous variables and (#) were within the accepted range (-2/+2). However, (#) variables (list variable name 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, income, marriage5)
 marriage5 
 are currently divorced from one another   are currently married to one another   never married each other and are not together   never married each other but are currently together 
 income 
   1 low  297 452 103 14
   2 middle  198 609 54 12
   3 high  72 446 7 4
   rather not say  164 590 75 15
   #Total cases  731 2097 239 45
# Note: for HW, replace the two variables with your project's categorical ones)
#cross_cases(d2, variable2, variable3)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$efficacy, d2$moa_maturity,
     main="Scatterplot of efficacy and moa_maturity",
     xlab = "efficacy",
     ylab = "moa_maturity")

plot(d2$stress, d2$support,
     main="Scatterplot of stress and support",
     xlab = "stress",
     ylab = "support")

# Note: for HW, you will choose to plot 2 combos of your 4 continuous variables, based on your research questions/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.

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 and income",
        xlab = "stress",
        ylab = "income")

boxplot(data=d2, support~marriage5,
        main="Boxplot of support and marriage5",
        xlab = "support",
        ylab = "marriage5")

# 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 research questions/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 graph.