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(stargazer)
## 
## Please cite as: 
## 
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
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
load("NSDUH_2023.Rdata")
nsduh_2023 <- data
cordata <- nsduh_2023 %>%
  select(BNGDRMDAYS, IRAMDEYR, AGE3) %>%
  drop_na() %>%
  filter(AGE3 %in% 4:11, IRAMDEYR %in% 0:1,
         BNGDRMDAYS %in% 1:5)
nrow(nsduh_2023)
## [1] 56705
nrow(cordata)
## [1] 45133
nrow(nsduh_2023) - nrow(cordata)
## [1] 11572
stopifnot(nrow(nsduh_2023) == 56705, nrow(cordata) == 45133)
stargazer(as.data.frame(cordata),
          type = "text",
          title = "Descriptive Statistics for Study Variables",
          summary.stat = c("n", "mean", "median", "sd", "min", "max"),
          digits = 3)
## 
## Descriptive Statistics for Study Variables
## ===============================================
## Statistic    N    Mean  Median St. Dev. Min Max
## -----------------------------------------------
## BNGDRMDAYS 45,133 4.135   5     1.525    1   5 
## IRAMDEYR   45,133 0.115   0     0.319    0   1 
## AGE3       45,133 7.817   8     2.194    4  11 
## -----------------------------------------------
characteristics <- data.frame(
  Characteristic = c("No major depressive episode",
                     "Past-year major depressive episode",
                     "18-20 years",
                     "21-23 years",
                     "24-25 years",
                     "26-29 years",
                     "30-34 years",
                     "35-49 years",
                     "50-64 years",
                     "65 years or older"),
  n = c(39948, 5185, 4728, 5214, 3645,
        3884, 5287, 12355, 4960, 5060)
)

characteristics$Percent <- round(characteristics$n / 45133 * 100, 2)

colnames(characteristics) <- c("Characteristic", "n", "%")

stargazer(characteristics,
          type = "text",
          summary = FALSE,
          rownames = FALSE,
          title = "Table 2: Unweighted Characteristics of the Adult Sample",
          digits = 2)
## 
## Table 2: Unweighted Characteristics of the Adult Sample
## ===============================================
## Characteristic                       n      %  
## -----------------------------------------------
## No major depressive episode        39,948 88.51
## Past-year major depressive episode 5,185  11.49
## 18-20 years                        4,728  10.48
## 21-23 years                        5,214  11.55
## 24-25 years                        3,645  8.08 
## 26-29 years                        3,884  8.61 
## 30-34 years                        5,287  11.71
## 35-49 years                        12,355 27.37
## 50-64 years                        4,960  10.99
## 65 years or older                  5,060  11.21
## -----------------------------------------------
binge <- data.frame(
  Characteristic = c("1-2 days",
                     "3-5 days",
                     "6-19 days",
                     "20-30 days",
                     "Non-user or no past-month alcohol use"),
  n = c(6827, 3107, 1736, 547, 44488)
)

binge$Percent <- round(binge$n / 56705 * 100, 2)

colnames(binge) <- c("Characteristic", "n", "%")

stargazer(binge,
          type = "text",
          summary = FALSE,
          rownames = FALSE,
          title = "Table 3: Distribution of Past-Month Binge Drinking",
          digits = 2)
## 
## Table 3: Distribution of Past-Month Binge Drinking
## ==================================================
## Characteristic                          n      %  
## --------------------------------------------------
## 1-2 days                              6,827  12.04
## 3-5 days                              3,107  5.48 
## 6-19 days                             1,736  3.06 
## 20-30 days                             547   0.96 
## Non-user or no past-month alcohol use 44,488 78.46
## --------------------------------------------------
model2 <- lm(BNGDRMDAYS ~ AGE3 + IRAMDEYR, data = cordata)
summary(model2)
## 
## Call:
## lm(formula = BNGDRMDAYS ~ AGE3 + IRAMDEYR, data = cordata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.3006 -0.2503  0.7999  0.9004  1.0990 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.747937   0.027227 137.655   <2e-16 ***
## AGE3         0.050239   0.003297  15.240   <2e-16 ***
## IRAMDEYR    -0.047917   0.022682  -2.113   0.0346 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.521 on 45130 degrees of freedom
## Multiple R-squared:  0.005534,   Adjusted R-squared:  0.00549 
## F-statistic: 125.6 on 2 and 45130 DF,  p-value: < 2.2e-16
stargazer(model2,type="text",digits=4)
## 
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                             BNGDRMDAYS         
## -----------------------------------------------
## AGE3                         0.0502***         
##                              (0.0033)          
##                                                
## IRAMDEYR                     -0.0479**         
##                              (0.0227)          
##                                                
## Constant                     3.7479***         
##                              (0.0272)          
##                                                
## -----------------------------------------------
## Observations                  45,133           
## R2                            0.0055           
## Adjusted R2                   0.0055           
## Residual Std. Error     1.5206 (df = 45130)    
## F Statistic         125.5670*** (df = 2; 45130)
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01
plot(model2, which = 1)

raintest(model2)
## 
##  Rainbow test
## 
## data:  model2
## Rain = 0.99131, df1 = 22567, df2 = 22563, p-value = 0.744
plot(model2, which = 2)

plot(model2, which = 3)

bptest(model2)
## 
##  studentized Breusch-Pagan test
## 
## data:  model2
## BP = 294.37, df = 2, p-value < 2.2e-16
dwtest(model2, alternative = "greater")
## 
##  Durbin-Watson test
## 
## data:  model2
## DW = 2.0031, p-value = 0.6306
## alternative hypothesis: true autocorrelation is greater than 0
vif(model2)
##     AGE3 IRAMDEYR 
## 1.021048 1.021048