data("wage1", package = "wooldridge") plot(wage ~ educ, data = wage1) # make it nicer library(tinyplot) plt(wage ~ educ, data = wage1, col = "red", pch = 21, bg = "orange", grid = TRUE, main = "wage vs educ", xlab = "educ", ylab = "wage") plt_add(type = "rug", side = 2) plt(~wage , data = wage1, type = "density") # nicer plt(~wage , data = wage1, type = "density", lw = 3, col = "red") # same plot but use log(wage) plt(log(wage) ~ educ, data = wage1, col = "red", pch = 21, bg = "orange", grid = TRUE, main = "log wage vs educ", xlab = "educ", ylab = "log wage") plt_add(type = "rug", side = 2) plt(~log(wage) , data = wage1, type = "density", lw = 3, col = "red") # run 3 models mods = list() mods[["level-level"]] = lm(wage ~ educ, data = wage1) mods[["log-level"]] = lm(log(wage) ~ educ, data = wage1) mods[["log-log"]] = lm(log(wage) ~ log(educ), data = wage1, subset = educ > 0) modelsummary::modelsummary(mods) # college tuition nonlinear x = read.csv("college_tuition_income.csv") plt(mid_career_pay ~ out_of_state_tuition, data = x) plt_add(type = "lm") plt_add(type=type_loess(), col = "blue") lm(mid_career_pay ~ out_of_state_tuition, data = x) x$pay1000 = x$mid_career_pay / 1000 x$tuition1000 = x$out_of_state_tuition / 1000 lm(pay1000 ~ tuition1000, data = x) # dividing LHS and RHS by the same number does not change anything! m = lm(pay1000 ~ tuition1000 + I(tuition1000^2), data = x) # predicted pay1000 for tuition1000 = 20? # a + bx + c x^2 83.76988 -0.33810 * 20 + 0.01692 * 20^2 # predicted pay1000 for tuition1000 = 10,20,30,40? nd = data.frame(tuition1000 = c(10:50)) nd$prediction = predict(m , newdata = nd) plt(prediction ~ tuition1000, data = nd, type = "l")