library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(janitor)
## 
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(haven)
library(readr)
library(ggplot2)
library(sur)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor
anes2020<-read_dta("C:\\Users\\Bryan\\Downloads\\anes2020.dta")
anes2020 %>%
tabyl(V202186)
anes2020 %>%
tabyl(V201600)
anes2020 %>%
tabyl(V201507x)
anes2020 %>%
tabyl(V201508)
anes2020 %>%
tabyl(V201509)
anes2020 <- filter(anes2020, V202186 >= 0 & V202186 <= 100)
anes2020 <- filter(anes2020, V201600 >= 1)
anes2020 <- filter(anes2020, V201507x >= 18)
anes2020 <- filter(anes2020, V201508 >= 1 & V201508 <= 6)
anes2020 <- filter(anes2020, V201509 >= 1 & V201509 <=2)
anes2020 %>%
ggplot(mapping = aes(V202186))+
geom_histogram()+
ggtitle(label="World Health Organziation Distribution")+
xlab(label="World Health Organziation Feelings")
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

anes2020 %>%
ggplot(mapping = aes(V202186, stat=..density..))+geom_density()+ggtitle(label="World Health Organziation Feelings Distribution")+xlab(label="World Health Organziation Feelings")
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.

qqnorm(anes2020$V202186)

ggplot(anes2020) + geom_point(mapping = aes(x=V201507x, y=V202186))
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.

scatter.smooth(anes2020$V201507x,anes2020$V202186)

anes2020 %>% 
  ggplot(mapping=aes(y=V202186, x=factor(V201600)))+
  geom_boxplot()+ 
  ggtitle(label="Distribution of World Health Organization Feelings by Gender") +
  xlab(label="World Health Organization")
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.

anes2020$gender.f <- factor(anes2020$V201600)
tapply(anes2020$V202186, anes2020$gender.f, mean)
##        1        2 
## 60.28822 67.11584
contr.treatment(2)
##   2
## 1 0
## 2 1
contrasts(anes2020$gender.f) = contr.treatment(2)
summary(lm(V202185~gender.f, anes2020))
## 
## Call:
## lm(formula = V202185 ~ gender.f, data = anes2020)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -89.22 -30.22  -1.60  19.78 927.40 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   71.601      3.026  23.659   <2e-16 ***
## gender.f2      8.615      3.949   2.181   0.0292 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 107.3 on 3041 degrees of freedom
## Multiple R-squared:  0.001562,   Adjusted R-squared:  0.001234 
## F-statistic: 4.759 on 1 and 3041 DF,  p-value: 0.02923
anes2020$cohab.f <- factor(anes2020$V201509)

contr.treatment(2)
##   2
## 1 0
## 2 1
summary(lm(V202185~cohab.f, anes2020))
## 
## Call:
## lm(formula = V202185 ~ cohab.f, data = anes2020)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -95.15 -24.44  -4.44  13.85 924.56 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   86.145      4.461  19.313   <2e-16 ***
## cohab.f2     -11.709      4.956  -2.363   0.0182 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 107.2 on 3041 degrees of freedom
## Multiple R-squared:  0.001832,   Adjusted R-squared:  0.001504 
## F-statistic: 5.582 on 1 and 3041 DF,  p-value: 0.01821
anes2020$relationship_status <-paste(anes2020$V201509, anes2020$V201508, sep = "" )
summary(anes2020$relationship_status)
##    Length     Class      Mode 
##      3043 character character
tabyl(anes2020$relationship_status)
anes2020$relations_coded <-car::Recode(anes2020$ relationship_status, recodes="'13 to 16' = 'Cohabitating'; '23 to 26' = 'Single'; else=NA", as.factor=T)


anes2020 %>% 
tabyl(relations_coded)
anes2020 %>%
  
ggplot(mapping=aes(y=V202186,x=factor(relations_coded)))+
  geom_boxplot()+
  ggtitle(label="World Health Organization Feelings by Relationship Type")+
  xlab(label="World Health Organization")
## Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/double. Defaulting to continuous.