library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library(MASS) 
## 
## Attaching package: 'MASS'
## 
## The following object is masked from 'package:dplyr':
## 
##     select
library(readxl)
load("NSDUH_2023.Rdata")
#data<-read_excel("NSDUH_2023.Rdata")
model1<-lm(CABINGFLG~IRMARIT,data=data)
model2<-lm(CABINGFLG~IRMARIT+SEXIDENT22+CATAGE,data=data)
model3<-lm(CABINGFLG~IRMARIT+SEXIDENT22+CATAGE+SEXATRACT2+IMPSOC+SUICTHNK,data=data)
summary(model1)
## 
## Call:
## lm(formula = CABINGFLG ~ IRMARIT, data = data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -0.602 -0.597 -0.585 -0.044 84.956 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.6079489  0.0236906  25.662  < 2e-16 ***
## IRMARIT     -0.0056991  0.0007594  -7.505 6.22e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.188 on 56703 degrees of freedom
## Multiple R-squared:  0.0009924,  Adjusted R-squared:  0.0009748 
## F-statistic: 56.33 on 1 and 56703 DF,  p-value: 6.225e-14
model1<-lm(CABINGFLG~IRMARIT+SEXIDENT22+CATAGE,data=data)
model3<-lm(CABINGFLG~IRMARIT+SEXATRACT2+SEXIDENT22+CATAGE+IMPSOC+SUICTHNK,data=data)
plot(model1,which=1)

plot(model2,which=1)
plot(model3,which=1)

raintest(model1)
## 
##  Rainbow test
## 
## data:  model1
## Rain = 1.1736, df1 = 28353, df2 = 28348, p-value < 2.2e-16
raintest(model2)
## 
##  Rainbow test
## 
## data:  model2
## Rain = 1.1736, df1 = 28353, df2 = 28348, p-value < 2.2e-16
raintest(model3)
## 
##  Rainbow test
## 
## data:  model3
## Rain = 1.1735, df1 = 28353, df2 = 28345, p-value < 2.2e-16
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
durbinWatsonTest(model1)
##  lag Autocorrelation D-W Statistic p-value
##    1     0.005743427      1.988513   0.226
##  Alternative hypothesis: rho != 0
durbinWatsonTest(model2)
##  lag Autocorrelation D-W Statistic p-value
##    1     0.005743427      1.988513   0.216
##  Alternative hypothesis: rho != 0
durbinWatsonTest(model3)
##  lag Autocorrelation D-W Statistic p-value
##    1     0.005878017      1.988244   0.208
##  Alternative hypothesis: rho != 0
plot(model1,which=3)

plot(model2,which=3)
plot(model3,which=3)

bptest(model1)
## 
##  studentized Breusch-Pagan test
## 
## data:  model1
## BP = 24.282, df = 3, p-value = 2.181e-05
bptest(model2)
## 
##  studentized Breusch-Pagan test
## 
## data:  model2
## BP = 24.282, df = 3, p-value = 2.181e-05
bptest(model3)
## 
##  studentized Breusch-Pagan test
## 
## data:  model3
## BP = 28.267, df = 6, p-value = 8.367e-05
plot(model1,which=2)

plot(model2,which=2)
plot(model3,which=2)

model2<-lm(CABINGFLG~IRMARIT,data=data)
model3<-lm(CABINGFLG~IRMARIT+SEXATRACT2+SEXIDENT22+CATAGE+IMPSOC+SUICTHNK,data=data)
vif(model1)
##    IRMARIT SEXIDENT22     CATAGE 
##   1.358172   1.000097   1.358092
vif(model3)
##    IRMARIT SEXATRACT2 SEXIDENT22     CATAGE     IMPSOC   SUICTHNK 
##   1.702838   3.874218   3.859798   2.162600   1.793826   3.758425
model3<-data %>% dplyr::select(CABINGFLG, SEXATRACT2, SEXIDENT22, CATAGE, IMPSOC,SUICTHNK)
cor(model3)
##               CABINGFLG  SEXATRACT2  SEXIDENT22       CATAGE      IMPSOC
## CABINGFLG   1.000000000 0.004871243 0.005210004  0.026001324 -0.02389414
## SEXATRACT2  0.004871243 1.000000000 0.859520749  0.001845605  0.15756520
## SEXIDENT22  0.005210004 0.859520749 1.000000000  0.001168330  0.16181291
## CATAGE      0.026001324 0.001845605 0.001168330  1.000000000 -0.40858320
## IMPSOC     -0.023894140 0.157565201 0.161812905 -0.408583200  1.00000000
## SUICTHNK   -0.034707310 0.218901394 0.215881056 -0.707459663  0.65982424
##               SUICTHNK
## CABINGFLG  -0.03470731
## SEXATRACT2  0.21890139
## SEXIDENT22  0.21588106
## CATAGE     -0.70745966
## IMPSOC      0.65982424
## SUICTHNK    1.00000000

```

Homework

  1. Load your preferred dataset into R studio - Complete
  2. Create a linear model “lm()” from the variables, with a continuous dependent variable as the outcome- Complete
  3. Check the following assumptions:
  1. Linearity (plot and raintest) - Through visual inpsectin all models appear to be linear by visiual inspection; however model 1 and model 3 have outlier possibly due to unanswered questions.
  2. Independence of errors (durbin-watson) - p-value > 0.05 - model probably doesn’t contain correlation of residuals
  3. Homoscedasticity (plot, bptest) - p-value > 0.05 - model is probably homoscedastic for all models
  4. Normality of residuals (QQ plot, shapiro test)- all models are normal by visual inspection, there are outliers possibly due to unanswered questions.
  5. No multicolinarity (VIF, cor) - complete
  1. does your model meet those assumptions? You don’t have to be perfectly right, just make a good case. The assumptions of indpendence of errors for all models
  2. If your model violates an assumption, which one?
  3. What would you do to mitigate this assumption? Show your work. I attempted to create new models however all models showed the same, there was no effect, the final result was still had non-independence of errors.