---
title: "HW09 -try"
author: "BJ"
date: "11/6/2021"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(grid)
library(gridExtra)
library(plyr)

theme_set(theme_bw())

cdi <- read.table("cdi.dat",header=T)
str(cdi)
```

### Part (a)

```{r}

g <- ggplot(cdi,aes(x=pct.hs.grad,y=per.cap.income)) + geom_point(pch=1) +
  facet_wrap( ~ state, as.table=F)

coef <- lm(per.cap.income ~ state - 1, data=cdi)$coef
J <- length(unique(cdi$state))
int <- rep(NA,J)
slo <- int
for (j in 1:J) {
    int[j] <- coef[j]                                           # ******
    slo[j] <- 0                                                 # ******
}

params <- data.frame(state=sort(unique(cdi$state)),int=int,slo=slo)

g <- g + geom_abline(data=params,
                     aes(intercept=int,slope=slo), color="blue")

plot(g)

```

### Part (b)

```{r}

g <- ggplot(cdi,aes(x=pct.hs.grad,y=per.cap.income)) + geom_point(pch=1) +
  facet_wrap( ~ state, as.table=F)

coef <- lm(per.cap.income ~ pct.hs.grad, data=cdi)$coef
J <- length(unique(cdi$state))
int <- rep(NA,J)
slo <- int
for (j in 1:J) {
    int[j] <- coef[1]                                           # ******
    slo[j] <- coef[2]                                                 # ******
}

params <- data.frame(state=sort(unique(cdi$state)),int=int,slo=slo)

g <- g + geom_abline(data=params,
                     aes(intercept=int,slope=slo), color="blue")

plot(g)

```

### Part (c)

```{r}

g <- ggplot(cdi,aes(x=pct.hs.grad,y=per.cap.income)) + geom_point(pch=1) +
  facet_wrap( ~ state, as.table=F)

coef <- lm(per.cap.income ~ pct.hs.grad + state - 1, data=cdi)$coef
J <- length(unique(cdi$state))
int <- rep(NA,J)
slo <- int
for (j in 1:J) {
    int[j] <- coef[j+1]                                           # ******
    slo[j] <- coef[1]                                                 # ******
}

params <- data.frame(state=sort(unique(cdi$state)),int=int,slo=slo)

g <- g + geom_abline(data=params,
                     aes(intercept=int,slope=slo), color="blue")

plot(g)

```

### Part (d)

```{r}

g <- ggplot(cdi,aes(x=pct.hs.grad,y=per.cap.income)) + geom_point(pch=1) +
  facet_wrap( ~ state, as.table=F)

coef <- lm(per.cap.income ~ pct.hs.grad * state - 1 - pct.hs.grad, data=cdi)$coef
J <- length(unique(cdi$state))
int <- rep(NA,J)
slo <- int
for (j in 1:J) {
    int[j] <- coef[j]                                          
    slo[j] <- coef[j+J]                                                
}

params <- data.frame(state=sort(unique(cdi$state)),int=int,slo=slo)

g <- g + geom_abline(data=params,
                     aes(intercept=int,slope=slo), color="blue")

plot(g)

```

### Part (d) [alt]

```{r}

g <- ggplot(cdi,aes(x=pct.hs.grad,y=per.cap.income)) + geom_point(pch=1) +
  facet_wrap( ~ state, as.table=F)

srt.state <- sort(unique(cdi$state))

J <- length(unique(cdi$state))
int <- rep(NA,J)
slo <- int
for (j in 1:J) {
      if(sum(cdi$state==srt.state[j])>1) {
      coef <- lm(per.cap.income ~ pct.hs.grad,data=cdi[cdi$state==srt.state[j],])$coef
      int[j] <- coef[1]                                           # ******
      slo[j] <- coef[2]                                                 # ******
    }
}

params <- data.frame(state=sort(unique(cdi$state)),int=int,slo=slo)

g <- g + geom_abline(data=params,
                     aes(intercept=int,slope=slo), color="blue")

plot(g)

```