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) # for the describe() command
library(expss) # for the cross_cases() command
## Loading required package: maditr
## 
## To aggregate data: take(mtcars, mean_mpg = mean(mpg), by = am)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by
## 
## Use 'expss_output_rnotebook()' to display tables inside R Notebooks.
##  To return to the console output, use 'expss_output_default()'.

##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$gender)
## 
##             female I use another term               male  Prefer not to say 
##               1005                 32                195                 20
table(d2$employment)
## 
## 1 high school equivalent     2 college/university               3 employed 
##                      850                       22                      302 
##             4 unemployed                5 retired        prefer not to say 
##                       58                        3                       17
# use histograms to visualize continuous data
hist(d2$big5_open)

hist(d2$iou)

hist(d2$rse)

hist(d2$pss)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars    n    mean      sd  median trimmed     mad min  max range
## X              1 1252 4764.43 2595.60 4989.50 4831.52 3351.42   1 8867  8866
## gender*        2 1252    1.38    0.80    1.00    1.21    0.00   1    4     3
## employment*    3 1252    1.72    1.12    1.00    1.53    0.00   1    6     5
## big5_open      4 1252    5.25    1.12    5.33    5.34    0.99   1    7     6
## iou            5 1252    2.57    0.91    2.41    2.51    0.99   1    5     4
## rse            6 1252    2.62    0.72    2.70    2.64    0.74   1    4     3
## pss            7 1252    2.95    0.95    3.00    2.95    1.11   1    5     4
##              skew kurtosis    se
## X           -0.17    -1.22 73.36
## gender*      1.74     1.40  0.02
## employment*  1.38     1.34  0.03
## big5_open   -0.74     0.43  0.03
## iou          0.49    -0.60  0.03
## rse         -0.21    -0.73  0.02
## pss          0.06    -0.76  0.03
## 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, gender, employment)
 employment 
 1 high school equivalent   2 college/university   3 employed   4 unemployed   5 retired   prefer not to say 
 gender 
   I use another term  27 3 1 1
   Prefer not to say  12 4 4
   female  658 18 265 52 1 11
   male  153 1 32 6 2 1
   #Total cases  850 22 302 58 3 17
# Note: for HW, replace the two variables with your project's categorical ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

plot(d2$big5_open, d2$rse,
     main="Scatterplot of big5_open and rse",
     xlab = "big5_open",
     ylab = "rse")

plot(d2$iou, d2$pss,
     main="Scatterplot of iou and pss",
     xlab = "iou",
     ylab = "pss")

# 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, pss~employment,
        main="Boxplot of employment and pss",
        xlab = "employment",
        ylab = "pss")

boxplot(data=d2, rse~gender,
        main="Boxplot of gender and rse",
        xlab = "gender",
        ylab = "rse")

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