-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegislators.Rmd
More file actions
133 lines (120 loc) · 4.28 KB
/
legislators.Rmd
File metadata and controls
133 lines (120 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
---
title: "R Notebook"
output: html_notebook
---
This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
```{r}
library(readstata13)
r13 <- read.dta13("washington_hw2.dta")
```
How many representatives are in the data set?
```{r}
nrow(r13)
```
What is the composition of the House in terms of sex and party (in percentages or proportions)?
```{r}
male<- subset(r13, female==0)
(nrow(male)/nrow(r13))*100
```
Female
```{r}
female<- subset(r13, female==1)
(nrow(female)/nrow(r13))*100
```
Democrats
```{r}
democrats<- subset(r13, party==1)
(nrow(democrats)/nrow(r13))*100
```
Republicans
```{r}
republicans<- subset(r13, party==2)
(nrow(republicans)/nrow(r13))*100
```
independents
```{r}
independents<- subset(r13, party==3)
(nrow(independents)/nrow(r13))*100
```
Summary of aauw
```{r}
summary(r13$aauw)
```
What percentage of representatives have a score of 100?
```{r}
score100<- subset(r13, aauw==100)
(nrow(score100)/nrow(r13))*100
```
What percentage of representatives have a score of 0?
```{r}
score0<- subset(r13, aauw==0)
(nrow(score0)/nrow(r13))*100
```
Run a regression
```{r}
model = lm(formula = aauw ~female, data = r13)
summary(model)
```
Provide a correct interpretation of the estimated coefficient
A positive coefficient indicates that as the value of the independent variable increases, the mean of the dependent variable also tends to increase. A negative coefficient suggests that as the independent variable increases, the dependent variable tends to decrease.So in our case we can conclude that there is a positive correlation between the independent and the dependent variables.
Provide a correct interpretation of the estimated intercept. Is the intercept a substantively
meaningful quantity in this context?
The intercept is meaningful in this context.
Calculate the predicted values of the AAUW’s legislative score for male legislators and for
female legislators.
```{r}
testpredict = predict(model, newdata = r13, type = 'response')
summary(testpredict)
```
Compare these predicted values from (c) with the average AAUW legislative score for male
legislators and the average AAUW legislative score for female legislators (you can calculate
these conditional averages using mean()).
```{r}
male.mean<-mean(male$aauw)
female.mean<-mean(female$aauw)
```
First, create a new binary variable called republican that is coded =1 if the legislator is a Republican and is coded=0 if the legislator is a Democrat or Independent
```{r}
z <- c(r13$party)
r13$republican<-ifelse(r13$party==2,1,0)
```
Run a regression
```{r}
model1 = lm(formula = aauw ~female+republican, data = r13)
summary(model1)
testpredict4 = predict(model1, newdata = r13, type = 'response')
summary(testpredict4)
```
4
```{r}
r13$female.republican<-ifelse(r13$female==1&r13$party==1,1,0)
model2 = lm(formula = aauw ~female+republican+female.republican, data = r13)
summary(model2)
testpredict2 = predict(model2, newdata = r13, type = 'response')
summary(testpredict2)
```
5
```{r}
model3 = lm(formula = aauw ~demvote, data = r13)
summary(model3)
```
Scatter Plot
```{r}
plot( r13$demvote,r13$aauw, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE)
# Add regression line
plot( r13$demvote,r13$aauw, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE)
abline(lm(aauw ~ demvote, data = r13), col = "blue")
```
e
```{r}
testpredict3 = predict(model3, newdata = r13, type = 'response')
summary(testpredict3)
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.