R Markdown

#statistical modeling

Get the current working directory to confirm it has been set correctly

getwd()

List the files within the directory to make sure we have all the documents needed

list.files()

Install necessary libraries if not already installed

if (!requireNamespace(“ggplot2”, quietly = TRUE)) { install.packages(“ggplot2”) } if (!requireNamespace(“dplyr”, quietly = TRUE)) { install.packages(“dplyr”) }

Load necessary libraries

library(ggplot2) library(dplyr)

Open PDF device to save the output

pdf(“student_enrollment_analysis.pdf”, width = 8, height = 6)

Read in the data

enrollment_data <- read.csv(“enrollmentForecast.csv”)

Look at the data structure

print(str(enrollment_data)) print(summary(enrollment_data))

Make scatterplots of ROLL against the other variables

ggplot(enrollment_data, aes(x = UNEM, y = ROLL)) + geom_point() + labs(title = “Enrollment vs. Unemployment Rate”, x = “Unemployment Rate (%)”, y = “Enrollment”)

ggplot(enrollment_data, aes(x = HGRAD, y = ROLL)) + geom_point() + labs(title = “Enrollment vs. High School Graduates”, x = “High School Graduates”, y = “Enrollment”)

ggplot(enrollment_data, aes(x = INC, y = ROLL)) + geom_point() + labs(title = “Enrollment vs. Per Capita Income”, x = “Per Capita Income ($1961)”, y = “Enrollment”)

Build a linear model using the unemployment rate (UNEM) and number of spring high school graduates (HGRAD)

fit_enrollment <- lm(ROLL ~ UNEM + HGRAD, data = enrollment_data)

Use the summary() function to investigate the model

print(summary(fit_enrollment))

Use the anova() function to investigate the model

print(anova(fit_enrollment))

The summary output shows the p-values; the variable with the smallest p-value is most closely related.

Make a residual plot and check for any bias in the model

plot(fit_enrollment, which = 1)

Use the predict() function to estimate the expected fall enrollment

if the current year’s unemployment rate is 9% and the size of the spring high school graduating class is 25,000 students

new_data <- data.frame(UNEM = 9, HGRAD = 25000) predicted_enrollment <- predict(fit_enrollment, new_data) print(predicted_enrollment)

Build a second model which includes per capita income (INC)

fit_enrollment_with_income <- lm(ROLL ~ UNEM + HGRAD + INC, data = enrollment_data)

Compare the two models with anova().

print(anova(fit_enrollment, fit_enrollment_with_income))

Close the PDF device

dev.off()

#To see if per capita incone improves the model, we use the ANOVA funciton to compare the two models: one without per captia income and one with. The ANOVA showed the following:The model without per capita income only includes the unemployment rate and the number of high school graduates as predictors for fall enrollment. The model with per capita income includes the per capita income, unemployment rate, and the number of highschool graduates as predictors for fall enrollment. When comparing these models using ANNOVA, looking at the f-statistics and the p-value to asses whether the inclusion of the additional variable significantly improves the model. If the ANNOVA shows a low p-value (typically 0.005) it means that including per capita income significantly improves the model. This suggests that the per captia income is a valuable predictor variable for enrollment. If the p-value is high, it means the per capita income is not a valuable predictor variable.