You are on page 1of 4

hw 6

Florencia Irene

2023-04-14

county_votes16 <- read.csv("county_votes16.csv")

Exercise 1

a.

glm1 <- glm(trump_win ~ obama_pctvotes, data = county_votes16)

summary(glm1)

##
## Call:
## glm(formula = trump_win ~ obama_pctvotes, data = county_votes16)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.8374 -0.1463 0.0378 0.1945 0.5597
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.4930638 0.0132007 113.11 <2e-16 ***
## obama_pctvotes -0.0168824 0.0003201 -52.74 <2e-16 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for gaussian family taken to be 0.06983762)
##
## Null deviance: 411.48 on 3111 degrees of freedom
## Residual deviance: 217.20 on 3110 degrees of freedom
## AIC: 552.63
##
## Number of Fisher Scoring iterations: 2

equation = log( 1− p̂ ) = 1.49 − 0.02obamapctvotes

b.
##
## Attaching package: ’dplyr’

1
## The following objects are masked from ’package:stats’:
##
## filter, lag

## The following objects are masked from ’package:base’:


##
## intersect, setdiff, setequal, union

## Warning: package ’ggplot2’ was built under R version 4.2.2

## ‘geom_smooth()‘ using formula = ’y ~ x’

1.00

0.75
probability trump win

0.50

0.25

0.00

0 25 50 75
obama_pctvotes

c.

predict(glm1, newdata = data.frame(obama_pctvotes = c(40,50,60)), type = "response")

## 1 2 3
## 0.8177677 0.6489437 0.4801197

2
d.

Interpretation: a one unit increase in obama_pctvotes associated with multiplicative change of

e−0.0169 = 0.983242

in the odds that trump wins.

Exercise 2

glm2 <- glm(trump_win ~ pct_pop65+ pct_black+ pct_white+ pct_hispanic+ pct_asian + highschool+ bachelors
summary(glm2)

##
## Call:
## glm(formula = trump_win ~ pct_pop65 + pct_black + pct_white +
## pct_hispanic + pct_asian + highschool + bachelors + income,
## data = county_votes16)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.03246 -0.09121 0.02121 0.16933 0.86572
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.6169233 0.1147771 5.375 8.23e-08 ***
## pct_pop65 0.0018401 0.0013263 1.387 0.165
## pct_black -0.0041141 0.0007587 -5.422 6.34e-08 ***
## pct_white 0.0059163 0.0007111 8.320 < 2e-16 ***
## pct_hispanic -0.0057150 0.0004500 -12.700 < 2e-16 ***
## pct_asian -0.0223928 0.0025586 -8.752 < 2e-16 ***
## highschool -0.0011717 0.0012411 -0.944 0.345
## bachelors -0.0183027 0.0009305 -19.669 < 2e-16 ***
## income 0.0058836 0.0006629 8.876 < 2e-16 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for gaussian family taken to be 0.07325065)
##
## Null deviance: 411.48 on 3111 degrees of freedom
## Residual deviance: 227.30 on 3103 degrees of freedom
## AIC: 708.1
##
## Number of Fisher Scoring iterations: 2

b.

pct_pop65 and highschool are not significant

3
glm3 <- glm(trump_win ~ pct_black+ pct_white+ pct_hispanic+ pct_asian + bachelors +income, data = coun
summary(glm3)

##
## Call:
## glm(formula = trump_win ~ pct_black + pct_white + pct_hispanic +
## pct_asian + bachelors + income, data = county_votes16)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.03767 -0.09227 0.02150 0.16945 0.86603
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.5548442 0.0675053 8.219 2.97e-16 ***
## pct_black -0.0039214 0.0007318 -5.358 9.01e-08 ***
## pct_white 0.0061383 0.0006953 8.828 < 2e-16 ***
## pct_hispanic -0.0056051 0.0003685 -15.211 < 2e-16 ***
## pct_asian -0.0220832 0.0025265 -8.740 < 2e-16 ***
## bachelors -0.0187149 0.0008111 -23.074 < 2e-16 ***
## income 0.0054792 0.0006052 9.053 < 2e-16 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## (Dispersion parameter for gaussian family taken to be 0.07325953)
##
## Null deviance: 411.48 on 3111 degrees of freedom
## Residual deviance: 227.47 on 3105 degrees of freedom
## AIC: 706.48
##
## Number of Fisher Scoring iterations: 2

c.

Interpretation of the sign: a. Negative sign


βj
(pct_black, pct_hispanic, pct_asian, bachelors): increases in one of these variables, while others remained
fixed will be associated with decreasing the probability that trump win.
a.Positive sign
βj
(pct_white and income): increases in pct_white or income while other variables remained fixed will be
associated with increasing the probability that trump win.

You might also like