Week 9 in class activity

Author

Jingyi Yang

library(haven)
library(descr)
Warning: package 'descr' was built under R version 4.4.3
library(MASS)#glm modeling 
library(rstatix) #Summary Tables 

Attaching package: 'rstatix'
The following object is masked from 'package:MASS':

    select
The following object is masked from 'package:stats':

    filter
library(tidyverse) #General coding 
Warning: package 'tidyverse' was built under R version 4.4.3
Warning: package 'ggplot2' was built under R version 4.4.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks rstatix::filter(), stats::filter()
✖ dplyr::lag()    masks stats::lag()
✖ dplyr::select() masks rstatix::select(), MASS::select()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(patchwork) #Merge GGPlots togeter 

Attaching package: 'patchwork'

The following object is masked from 'package:MASS':

    area
library(ggplot2) #Graphing
library(stargazer) #Tabular Regression Results

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(jtools) #Tabular Regression Results
Warning: package 'jtools' was built under R version 4.4.3
library(descr) #Easy Frequency Tables  
library(haven) #Imports survey data 
library(stats) #Various statistical calculations 
library(ggeffects) #Predicted Probabilities from Regressions
Warning: package 'ggeffects' was built under R version 4.4.3
library(pscl) #Zero Inflated Negative Binomial
Classes and Methods for R originally developed in the
Political Science Computational Laboratory
Department of Political Science
Stanford University (2002-2015),
by and under the direction of Simon Jackman.
hurdle and zeroinfl functions by Achim Zeileis.

Load-in data

anes_2020_count  <- read_dta("C:/Users/Admin/Downloads/anes_2020_count.dta")
head(anes_2020_count)
# A tibble: 6 × 1,780
  version V200001 V160001_orig V200002 V200003 V200004 V200005 V200006  V200007 
  <chr>     <dbl> <dbl+lbl>    <dbl+l> <dbl+l> <dbl+l> <dbl+l> <dbl+lb> <dbl+lb>
1 ANES20…  200015 401318       3 [3. … 2 [2. … 3 [3. … 0 [0. … -2 [-2.… -2 [-2.…
2 ANES20…  200039 400181       3 [3. … 2 [2. … 3 [3. … 0 [0. … -2 [-2.… -2 [-2.…
3 ANES20…  200053 405145       3 [3. … 2 [2. … 3 [3. … 1 [1. … -2 [-2.… -2 [-2.…
4 ANES20…  200114 402782       3 [3. … 2 [2. … 3 [3. … 1 [1. …  4 [4. … -1 [-1.…
5 ANES20…  200138 300209       3 [3. … 2 [2. … 3 [3. … 0 [0. … -2 [-2.… -2 [-2.…
6 ANES20…  200206 301799       3 [3. … 2 [2. … 3 [3. … 0 [0. … -2 [-2.… -2 [-2.…
# ℹ 1,771 more variables: V200008 <dbl+lbl>, V200009 <dbl+lbl>, V200010a <dbl>,
#   V200010b <dbl>, V200010c <dbl>, V200010d <dbl>, V200011a <dbl>,
#   V200011b <dbl>, V200011c <dbl>, V200011d <dbl>, V200012a <dbl>,
#   V200012b <dbl>, V200012c <dbl>, V200012d <dbl>, V200013a <dbl>,
#   V200013b <dbl>, V200013c <dbl>, V200013d <dbl>, V200014a <dbl>,
#   V200014b <dbl>, V200014c <dbl>, V200014d <dbl>, V200015a <dbl>,
#   V200015b <dbl>, V200015c <dbl>, V200015d <dbl>, V200016a <dbl>, …
anes_data<- anes_2020_count %>% select(rw_shows, libcon, pid_x, V201005, V202543, poc, education, female, V200010b)

head(anes_data)
# A tibble: 6 × 9
  rw_shows libcon         pid_x V201005 V202543    poc education female V200010b
     <dbl> <dbl+lbl>      <dbl> <dbl+l> <dbl+lb> <dbl> <dbl+lbl>  <dbl>    <dbl>
1        3  6 [6. Conser…     7 2 [2. …  4 [4. …     1 3 [BA/BS]      0    1.01 
2        0  2 [2. Libera…     3 1 [1. …  4 [4. …     0 1 [HS or…      1    0.769
3        1  5 [5. Slight…     4 2 [2. … NA           1 4 [Advan…      0    0.966
4        0 NA                 2 2 [2. …  4 [4. …     1 2 [Some …      0    1.84 
5        0  4 [4. Modera…     2 4 [4. …  5 [5. …     0 1 [HS or…      0    1.25 
6        0  3 [3. Slight…     1 2 [2. …  5 [5. …     0 3 [BA/BS]      0    0.742
anes_data %>%
  summarize(
    var_rw_shows = var(rw_shows, na.rm = TRUE),
    mean_rw_shows = mean(rw_shows, na.rm = TRUE),
    ratio = var_rw_shows / mean_rw_shows)
# A tibble: 1 × 3
  var_rw_shows mean_rw_shows ratio
         <dbl>         <dbl> <dbl>
1         1.50         0.536  2.81
quasi_all <- glm(rw_shows ~ libcon+pid_x+V201005+V202543+poc+education+female, family = quasipoisson, data = anes_data, weights = anes_data$V200010b)
summary(quasi_all)

Call:
glm(formula = rw_shows ~ libcon + pid_x + V201005 + V202543 + 
    poc + education + female, family = quasipoisson, data = anes_data, 
    weights = anes_data$V200010b)

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) -3.405738   0.339126 -10.043  < 2e-16 ***
libcon       0.542642   0.050346  10.778  < 2e-16 ***
pid_x        0.192327   0.037853   5.081 4.07e-07 ***
V201005     -0.573236   0.049605 -11.556  < 2e-16 ***
V202543      0.026170   0.046521   0.563    0.574    
poc         -0.148317   0.125122  -1.185    0.236    
education    0.003585   0.039537   0.091    0.928    
female      -0.048341   0.078526  -0.616    0.538    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for quasipoisson family taken to be 1.565885)

    Null deviance: 3562.2  on 2216  degrees of freedom
Residual deviance: 2059.3  on 2209  degrees of freedom
  (1284 observations deleted due to missingness)
AIC: NA

Number of Fisher Scoring iterations: 6
nb_all <- glm.nb(rw_shows ~ libcon+pid_x+V201005+V202543+poc+education+female, data= anes_data, weights = anes_data$V200010b)
summary(nb_all) 

Call:
glm.nb(formula = rw_shows ~ libcon + pid_x + V201005 + V202543 + 
    poc + education + female, data = anes_data, weights = anes_data$V200010b, 
    init.theta = 0.7877996507, link = log)

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept) -3.56722    0.37922  -9.407  < 2e-16 ***
libcon       0.57969    0.05612  10.330  < 2e-16 ***
pid_x        0.20718    0.04008   5.169 2.35e-07 ***
V201005     -0.52377    0.05439  -9.629  < 2e-16 ***
V202543      0.01078    0.05772   0.187   0.8519    
poc         -0.29017    0.14091  -2.059   0.0395 *  
education   -0.04527    0.04860  -0.931   0.3516    
female      -0.06987    0.09595  -0.728   0.4665    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for Negative Binomial(0.7878) family taken to be 1)

    Null deviance: 2091.8  on 2216  degrees of freedom
Residual deviance: 1176.7  on 2209  degrees of freedom
  (1284 observations deleted due to missingness)
AIC: 2992.8

Number of Fisher Scoring iterations: 1

              Theta:  0.7878 
          Std. Err.:  0.0907 

 2 x log-likelihood:  -2974.7880 
stargazer(quasi_all, nb_all, type="text", diits=3)

====================================================
                         Dependent variable:        
                  ----------------------------------
                               rw_shows             
                  glm: quasipoisson     negative    
                     link = log         binomial    
                         (1)              (2)       
----------------------------------------------------
libcon                0.543***          0.580***    
                       (0.050)          (0.056)     
                                                    
pid_x                 0.192***          0.207***    
                       (0.038)          (0.040)     
                                                    
V201005               -0.573***        -0.524***    
                       (0.050)          (0.054)     
                                                    
V202543                 0.026            0.011      
                       (0.047)          (0.058)     
                                                    
poc                    -0.148           -0.290**    
                       (0.125)          (0.141)     
                                                    
education               0.004            -0.045     
                       (0.040)          (0.049)     
                                                    
female                 -0.048            -0.070     
                       (0.079)          (0.096)     
                                                    
Constant              -3.406***        -3.567***    
                       (0.339)          (0.379)     
                                                    
----------------------------------------------------
Observations            2,217            2,217      
Log Likelihood                         -1,488.394   
theta                               0.788*** (0.091)
Akaike Inf. Crit.                      2,992.788    
====================================================
Note:                    *p<0.1; **p<0.05; ***p<0.01

=
3
-

According to the result, “Dispersion parameter for quasipoisson family taken to be 1.565885,” which is higher than 1, so the negative binomial will be more suitable.

According to the result, for both model, 1) people who are conservative are more likely to consume right-wing media resource, 2) people who lean to the republican are more likely to consume the right-wing media resource.