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 several columns with one summary: take(mtcars, mpg, hp, fun = mean, by = am)
## 
## Attaching package: 'maditr'
## The following object is masked from 'package:base':
## 
##     sort_by

##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)
## 
##    f    m   nb 
## 2315  787   53
table(d2$sibling)
## 
## at least one sibling           only child 
##                 2853                  302
# use histograms to visualize continuous data
hist(d2$idea)

hist(d2$belong)

hist(d2$efficacy)

hist(d2$npi)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##             vars    n    mean     sd  median trimmed     mad min  max  range
## ResponseId*    1 3155 1578.00 910.91 1578.00 1578.00 1169.77 1.0 3155 3154.0
## gender*        2 3155    1.28   0.49    1.00    1.21    0.00 1.0    3    2.0
## sibling*       3 3155    1.10   0.29    1.00    1.00    0.00 1.0    2    1.0
## idea           4 3155    3.57   0.38    3.62    3.62    0.37 1.0    4    3.0
## belong         5 3155    3.23   0.60    3.30    3.25    0.59 1.3    5    3.7
## efficacy       6 3155    3.13   0.45    3.10    3.13    0.44 1.1    4    2.9
## npi            7 3155    0.28   0.31    0.15    0.24    0.23 0.0    1    1.0
##              skew kurtosis    se
## ResponseId*  0.00    -1.20 16.22
## gender*      1.39     0.87  0.01
## sibling*     2.75     5.55  0.01
## idea        -1.49     4.14  0.01
## belong      -0.26    -0.12  0.01
## efficacy    -0.26     0.51  0.01
## npi          0.94    -0.69  0.01
## 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 (5) were within the accepted range (-2/+2). However, (1) variables (sibling) were outside of the accepted range. For this analysis, we will use them anyway, but outside of this class this is bad practice.

Bivariate Plots

Crosstabs

Crosstabs are used to visualize combinations of two categorical variables.

cross_cases(d2, gender, sibling)
 sibling 
 at least one sibling   only child 
 gender 
   f  2099 216
   m  706 81
   nb  48 5
   #Total cases  2853 302
# 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$idea, d2$belong,
     main="Scatterplot of idea and belong",
     xlab = "idea",
     ylab = "belong")

plot(d2$efficacy, d2$npi,
     main="Scatterplot of efficacy and npi",
     xlab = "efficacy",
     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, efficacy~gender,
        main="Boxplot of gender and efficacy",
        xlab = "gender",
        ylab = "efficacy")

boxplot(data=d2, belong~sibling,
        main="Boxplot of sibling and belong",
        xlab = "sibling",
        ylab = "belong")

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