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 "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$ethnicity)
## 
##  Asian/Asian British - Indian, Pakistani, Bangladeshi, other 
##                                                           35 
##              Black/Black British - Caribbean, African, other 
##                                                           12 
##                                      Chinese/Chinese British 
##                                                            3 
## Middle Eastern/Middle Eastern British - Arab, Turkish, other 
##                                                            4 
##                                           Mixed race - other 
##                                                           18 
##                   Mixed race - White and Black/Black British 
##                                                           10 
##                                           Other ethnic group 
##                                                            3 
##                                            Prefer not to say 
##                                                           10 
##                                White - British, Irish, other 
##                                                          322
table(d2$gender)
## 
##             female I use another term               male  Prefer not to say 
##                325                 25                 59                  8
# use histograms to visualize continuous data
hist(d2$gad)

hist(d2$rse)

hist(d2$brs)

hist(d2$pss)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##            vars   n    mean     sd  median trimmed    mad  min  max range  skew
## X             1 417 7555.63 742.18 7488.00 7549.74 937.00 6291 8867  2576  0.07
## ethnicity*    2 417    7.75   2.59    9.00    8.41   0.00    1    9     8 -1.85
## gender*       3 417    1.40   0.80    1.00    1.23   0.00    1    4     3  1.72
## gad           4 417    2.54   0.93    2.57    2.55   1.27    1    4     3 -0.04
## rse           5 417    2.24   0.70    2.20    2.22   0.74    1    4     3  0.33
## brs           6 417    2.67   0.88    2.67    2.67   0.99    1    5     4  0.10
## pss           7 417    3.42   0.92    3.50    3.46   1.11    1    5     4 -0.41
##            kurtosis    se
## X             -1.20 36.34
## ethnicity*     1.82  0.13
## gender*        1.47  0.04
## gad           -1.19  0.05
## rse           -0.65  0.03
## brs           -0.68  0.04
## pss           -0.58  0.04
## 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, gender, ethnicity)
 ethnicity 
 Asian/Asian British - Indian, Pakistani, Bangladeshi, other   Black/Black British - Caribbean, African, other   Chinese/Chinese British   Middle Eastern/Middle Eastern British - Arab, Turkish, other   Mixed race - other   Mixed race - White and Black/Black British   Other ethnic group   Prefer not to say   White - British, Irish, other 
 gender 
   I use another term  2 1 2 20
   Prefer not to say  1 3 4
   female  28 11 3 3 12 10 3 7 248
   male  5 1 3 50
   #Total cases  35 12 3 4 18 10 3 10 322
# Note: for HW, replace the two lab variables with your project ones)

Scatterplots

Scatterplots are used to visualize combinations of two continuous variables.

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

plot(d2$gad, d2$brs,
     main="Scatterplot of gad and brs",
     xlab = "gad",
     ylab = "brs")

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

boxplot(data=d2, gad~ethnicity,
        main="Boxplot of gad and ethnicity",
        xlab = "gad",
        ylab = "ethnicity")

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