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)
## 
## 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$urban_rural)
## 
##              city isolated dwelling              town           village 
##               126                11               166                87
table(d2$education)
## 
##              1 equivalent to not completing high school 
##                                                      94 
##                  2 equivalent to high school completion 
##                                                     178 
## 3 equivalent to vocational/technical program completion 
##                                                       3 
##                        4 equivalent to AP/IB completion 
##                                                      66 
##                                  5 undergraduate degree 
##                                                      15 
##                             6 graduate degree or higher 
##                                                       8 
##                                       prefer not to say 
##                                                      26
# use histograms to visualize continuous data
hist(d2$mfq_state)

hist(d2$swemws)

hist(d2$brs)

hist(d2$edeq12)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##              vars   n    mean     sd  median trimmed    mad  min  max range
## X               1 390 7558.81 738.53 7499.00 7555.19 945.16 6291 8860  2569
## urban_rural*    2 390    2.55   1.16    3.00    2.56   1.48    1    4     3
## education*      3 390    2.64   1.69    2.00    2.35   1.48    1    7     6
## edeq12          4 390    2.11   0.80    2.00    2.07   0.99    1    4     3
## mfq_state       5 390    3.74   1.01    3.81    3.77   1.02    1    6     5
## swemws          6 390    2.79   0.86    2.71    2.79   0.85    1    5     4
## brs             7 390    2.69   0.88    2.67    2.69   0.99    1    5     4
##               skew kurtosis    se
## X             0.04    -1.20 37.40
## urban_rural* -0.31    -1.41  0.06
## education*    1.25     0.69  0.09
## edeq12        0.34    -1.00  0.04
## mfq_state    -0.28    -0.29  0.05
## swemws        0.06    -0.48  0.04
## brs           0.09    -0.65  0.04
## 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, urban_rural, education)
 education 
 1 equivalent to not completing high school   2 equivalent to high school completion   3 equivalent to vocational/technical program completion   4 equivalent to AP/IB completion   5 undergraduate degree   6 graduate degree or higher   prefer not to say 
 urban_rural 
   city  33 51 2 18 8 2 12
   isolated dwelling  1 3 5 1 1
   town  41 72 33 4 6 10
   village  19 52 1 10 2 3
   #Total cases  94 178 3 66 15 8 26
# 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$mfq_state, d2$swemws,
     main="Scatterplot of mfq_state and swemws",
     xlab = "mfq_state",
     ylab = "swemws")

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

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

boxplot(data=d2, edeq12~education,
        main="Boxplot of education and edeq12",
        xlab = "education",
        ylab = "edeq12")

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