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 all non-grouping columns: take_all(mtcars, mean, by = am)

##Import Data

# Import the "projectdata.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$edu)
## 
##      1 High school diploma or less, and NO COLLEGE 
##                                                 58 
##                             2 Currently in college 
##                                               2537 
## 3 Completed some college, but no longer in college 
##                                                 35 
##                   4 Complete 2 year College degree 
##                                                178 
##                       5 Completed Bachelors Degree 
##                                                137 
##                  6 Currently in graduate education 
##                                                133 
##                   7 Completed some graduate degree 
##                                                 59
table(d2$party_rc)
## 
##  apolitical    democrat independent  republican 
##         436        1594         325         782
table(d2$usdream)
## 
##               american dream is important and achievable for me 
##                                                            1455 
##     american dream is important but maybe not achievable for me 
##                                                             341 
## american dream is not important and maybe not achievable for me 
##                                                             580 
##        american dream is not important but is achievable for me 
##                                                             181 
##                            not sure if american dream important 
##                                                             580
# use histograms to visualize continuous data
hist(d2$swb)

hist(d2$belong)

hist(d2$npi)

US Dream is being used as a categorical so it will be in that category rather than in the continuous one. ## Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars    n    mean     sd  median trimmed     mad min  max  range
## ResponseID*    1 3137 1569.00 905.72 1569.00 1569.00 1162.36 1.0 3137 3136.0
## edu*           2 3137    2.50   1.24    2.00    2.17    0.00 1.0    7    6.0
## party_rc*      3 3137    2.46   1.01    2.00    2.45    0.00 1.0    4    3.0
## swb            4 3137    4.48   1.32    4.67    4.53    1.48 1.0    7    6.0
## belong         5 3137    3.23   0.60    3.30    3.25    0.59 1.3    5    3.7
## usdream*       6 3137    2.39   1.55    2.00    2.24    1.48 1.0    5    4.0
## npi            7 3137    0.28   0.31    0.15    0.24    0.23 0.0    1    1.0
##              skew kurtosis    se
## ResponseID*  0.00    -1.20 16.17
## edu*         2.20     3.76  0.02
## party_rc*    0.42    -1.04  0.02
## swb         -0.36    -0.47  0.02
## belong      -0.25    -0.13  0.01
## usdream*     0.62    -1.13  0.03
## npi          0.94    -0.68  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 continuous variables and all were within the accepted range (-2/+2).

## OPTION 2
# We analyzed the skew and kurtosis of our continuous variables and (#) were within the accepted range (-2/+2). However, (#) variables (list variable name(s) 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, edu, party_rc)
 party_rc 
 apolitical   democrat   independent   republican 
 edu 
   1 High school diploma or less, and NO COLLEGE  13 28 4 13
   2 Currently in college  338 1268 260 671
   3 Completed some college, but no longer in college  6 17 6 6
   4 Complete 2 year College degree  28 86 26 38
   5 Completed Bachelors Degree  19 78 11 29
   6 Currently in graduate education  25 77 13 18
   7 Completed some graduate degree  7 40 5 7
   #Total cases  436 1594 325 782
# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables. Instead of using my hypotheses with the American Dream, I swapped out “usdream” for a different variable.

plot(d2$swb, d2$belong,
     main="Scatterplot of Satisfaction with Life Scale and Need to Belong",
     xlab = "swb",
     ylab = "belong")

plot(d2$belong, d2$npi,
     main="Scatterplot of Need to Belong and Narcissism",
     xlab = "belong",
     ylab = "npi")

# 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, swb~edu,
        main="Boxplot of Education and  Satisfaction with life ",
        xlab = "edu",
        ylab = "swb")

boxplot(data=d2, npi~party_rc,
        main="Boxplot of Political Party and Narcissistic Personality Inventory ",
        xlab = "party_rc",
        ylab = "npi")

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

We did it!!