aboutsummaryrefslogtreecommitdiff
path: root/R_LinR/main.r
blob: 3bb98fbbcb97b7b6ab3766b1ef161c0fdb8805e9 (plain)
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
#!/usr/bin/env Rscript

df <- read.csv("zeta.csv")
df <- subset(df, sex == "F")
df <- subset(df, select = -c(zcta, sex))
df <- subset(df, 8 < meaneducation & meaneducation < 18)
df <- subset(df, 10000 < meanhouseholdincome & meanhouseholdincome < 200000)
df <- subset(df, 0 < meanemployment & meanemployment < 3)
df <- subset(df, 20 < meanage & meanage < 60)

df$log_income <- log10(df$meanhouseholdincome)


names(df) <- c("X", "age", "education", "employment", "income", "log_income")

library(ggplot2)

# b

model <- lm(log_income ~ age, df)

# a

ggplot(df, aes(x = age, y = log_income)) +
  geom_point(alpha = 0.2) +
  geom_abline(
    intercept = coef(model)[1],
    slope = coef(model)[2],
    color = "red",
    size = 1
  ) +
  labs(x = "age", y = "income", title = "log_income(age)")

# bcde
print(model)
summary(model)

model_2 <- lm(log_income ~ education, df)
print(model_2)
summary(model_2)

ggplot(df, aes(x = education, y = log_income)) +
  geom_point(alpha = 0.2) +
  geom_abline(
    intercept = coef(model_2)[1],
    slope = coef(model_2)[2],
    color = "red",
    size = 1
  ) +
  labs(x = "education", y = "income", title = "log_income(education)")


model_3 <- lm(log_income ~ education + age + employment, df)
print(model_3)
summary(model_3)

ggplot(df) +
  geom_point(aes(x = log_income, y = fitted(model_3)), alpha = 0.2) +
  geom_line(aes(x = log_income, y = log_income), col = "red") +
  labs(x = "actual", y = "predicted")