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 get total summary skip 'by' argument: take_all(mtcars, mean)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by

##Import Data

# Import the "projectdat.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$age)
## 
## 1 between 18 and 25 2 between 26 and 35 3 between 36 and 45           4 over 45 
##                 507                  32                  11                   7
table(d2$disability)
## 
## chronic health       learning          other       physical    psychiatric 
##             93             82             61             28            244 
##        sensory 
##             49
# use histograms to visualize continuous data
hist(d2$swb)

hist(d2$belong)

hist(d2$socmeduse)

hist(d2$npi)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars   n   mean     sd median trimmed    mad  min max range  skew
## ResponseId*    1 557 279.00 160.94 279.00  279.00 206.08  1.0 557 556.0  0.00
## age*           2 557   1.13   0.48   1.00    1.00   0.00  1.0   4   3.0  4.11
## disability*    3 557   3.71   1.71   5.00    3.77   1.48  1.0   6   5.0 -0.42
## socmeduse      4 557  33.66   8.83  34.00   33.92   8.90 11.0  55  44.0 -0.29
## npi            5 557   0.27   0.30   0.15    0.23   0.23  0.0   1   1.0  0.93
## belong         6 557   3.28   0.61   3.30    3.29   0.59  1.5   5   3.5 -0.18
## swb            7 557   4.00   1.41   4.17    4.04   1.48  1.0   7   6.0 -0.18
##             kurtosis   se
## ResponseId*    -1.21 6.82
## age*           17.81 0.02
## disability*    -1.36 0.07
## socmeduse       0.16 0.37
## npi            -0.75 0.01
## belong         -0.32 0.03
## swb            -0.79 0.06
## 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, age, disability)
 disability 
 chronic health   learning   other   physical   psychiatric   sensory 
 age 
   1 between 18 and 25  81 75 53 26 227 45
   2 between 26 and 35  7 4 4 2 11 4
   3 between 36 and 45  2 2 4 3
   4 over 45  3 1 3
   #Total cases  93 82 61 28 244 49
# 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$swb, d2$socmeduse,
     main="Scatterplot of swb and socmeduse",
     xlab = "swb",
     ylab = "socmeduse")

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

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

boxplot(data=d2, swb~disability,
        main="Boxplot of swb and disability",
        xlab = "swb",
        ylab = "disability")

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