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
## 
## Use magrittr pipe '%>%' to chain several operations:
##              mtcars %>%
##                  let(mpg_hp = mpg/hp) %>%
##                  take(mean(mpg_hp), 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)
## 
##             female I use another term               male  Prefer not to say 
##                851                 32                164                 19
table(d2$exercise)
## 
##      0    0.1   0.15    0.2   0.25    0.3   0.35   0.45    0.5   0.57    0.6 
##     74      4      1      2      7      5      1      4     70      1      4 
##   0.66    0.7   0.72   0.75    0.8    0.9      1    1.1    1.2   1.25    1.3 
##      1      7      1     12      4      1    195      2      6      8      5 
##   1.45    1.5   1.55    1.6    1.7   1.75      2    2.1    2.2   2.25    2.3 
##      3    115      1      1      2      5    181      1      1      3      1 
##    2.5   2.75      3    3.1    3.2   3.25    3.5      4   4.25    4.5      5 
##     59      3     76      1      1      2     26     40      1      8     27 
##    5.5      6    6.3    6.5      7    7.3    7.5      8    8.5      9     10 
##      6     17      1      3     13      1      1      8      1      6      8 
##  10.25     11     12     13     14     15     16     17   17.5     18  31.75 
##      1      4      5      3      4      1      2      1      1      2      1 
##     32   40.3 333333 
##      1      1      1
# use histograms to visualize continuous data
hist(d2$big5_ext)

hist(d2$big5_neu)

hist(d2$rse)

hist(d2$edeq12)

Univariate Normality for Continuous Variables (individually)

describe(d2)
##          vars    n    mean       sd  median trimmed     mad min    max  range
## X           1 1066 4622.99  2585.12 4686.00 4662.86 3324.73   1   8854   8853
## gender*     2 1066    1.39     0.81    1.00    1.22    0.00   1      4      3
## exercise    3 1066  315.11 10209.31    1.52    1.87    1.07   0 333333 333333
## big5_ext    4 1066    4.34     1.46    4.67    4.39    1.48   1      7      6
## big5_neu    5 1066    4.35     1.51    4.67    4.39    1.48   1      7      6
## rse         6 1066    2.65     0.71    2.70    2.67    0.74   1      4      3
## edeq12      7 1066    1.87     0.73    1.67    1.80    0.74   1      4      3
##           skew kurtosis     se
## X        -0.09    -1.22  79.18
## gender*   1.73     1.40   0.02
## exercise 32.56  1059.01 312.69
## big5_ext -0.27    -0.82   0.04
## big5_neu -0.29    -0.79   0.05
## rse      -0.25    -0.66   0.02
## edeq12    0.73    -0.48   0.02
## 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, gender, exercise)
 exercise 
 0   0.1   0.15   0.2   0.25   0.3   0.35   0.45   0.5   0.57   0.6   0.66   0.7   0.72   0.75   0.8   0.9   1   1.1   1.2   1.25   1.3   1.45   1.5   1.55   1.6   1.7   1.75   2   2.1   2.2   2.25   2.3   2.5   2.75   3   3.1   3.2   3.25   3.5   4   4.25   4.5   5   5.5   6   6.3   6.5   7   7.3   7.5   8   8.5   9   10   10.25   11   12   13   14   15   16   17   17.5   18   31.75   32   40.3   333333 
 gender 
   I use another term  7 1 2 8 1 6 2 3 1 1
   Prefer not to say  4 1 1 4 3 3 1 1 1
   female  51 3 1 6 4 1 4 59 1 4 6 1 10 4 147 2 5 7 5 3 100 1 2 4 139 1 1 2 1 50 1 54 1 22 34 1 8 25 5 14 2 10 1 1 8 1 4 8 1 4 5 3 3 2 1 1 2 1 1 1 1
   male  12 1 1 1 8 1 1 2 1 36 1 1 12 1 33 1 7 2 19 1 1 1 3 5 2 1 2 3 2 1 1
   #Total cases  74 4 1 2 7 5 1 4 70 1 4 1 7 1 12 4 1 195 2 6 8 5 3 115 1 1 2 5 181 1 1 3 1 59 3 76 1 1 2 26 40 1 8 27 6 17 1 3 13 1 1 8 1 6 8 1 4 5 3 4 1 2 1 1 2 1 1 1 1
# 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$big5_neu, d2$edeq12,
     main="Scatterplot of Neuroticism and Disordered Eating",
     xlab = "Neuroticism",
     ylab = "Disordered Eating")

plot(d2$big5_ext, d2$edeq12,
     main="Scatterplot of Extroversion and Disordered Eating",
     xlab = "Extroversion",
     ylab = "Disordered Eating")

# 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, edeq12~gender,
        main="Boxplot of Disordered Eating and Exercise",
        xlab = "Gender",
        ylab = "Disordered Eating")

boxplot(data=d2, rse~gender,
        main="Boxplot of self esteem and gender",
        xlab = "Gender",
        ylab = "Self Esteem")

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