Regression

Model script

#install.packages("tidyverse")  # uncomment if tidyverse not installed
#install.packages("rio")  # uncomment if rio not installed
library(tidyverse)
library(rio)

# load in and prepare the data (using code seen previously)
  data <- import("https://learndata.net/nycschools.dta")
  data <- data |> mutate(iep = parse_number(iep))
  data <- data |> mutate(blackhispanic = parse_number(blackhispanic))
  data <- data |> mutate(ell = parse_number(ell))
  data <- data |> mutate(thgrmathela = parse_number(thgrmathela))
  data <- data |> rename(combined_score = thgrmathela)

# run a regression, then show a summary of its results
regression1 <- lm(overallscore ~ combined_score, data = data)
summary(regression1)

# you can also combine the above into a single line
summary(lm(overallscore ~ combined_score, data = data))

# another regression
regression2 <- lm(overallscore ~ combined_score + ell + iep +
  blackhispanic + economicneedindex, data = data)
summary(regression2)

# if you prefer R to avoid scientific notation (when practical),
# you can change the scipen option in R to a higher value
# (0 is the default; here we set it to 10)
options(scipen = 10)

# now, see how scientific notation is barely used in the results
summary(regression2)

Key concepts/syntax:

  1. lm(): run a linear (OLS) regression

    regression1 <- lm(overallscore ~ combined_score, data = data)
  2. summary(): show a summary, such as a summary of regression results

    summary(regression1)
  3. options(): change general options in R, such as scientific notation display settings

    options(scipen = 10)