set.seed(1030)
xsquared <- function(x) {
x^2
}
# Generate xy data
sim_data <- function(xsquared, sample_size = 100) {
x <- runif(n = sample_size, min = 0, max = 1)
y <- rnorm(n = sample_size, mean = xsquared(x), sd = 0.05)
data.frame(x, y)
}
# Generate predicted data (model)
df <- sim_data(xsquared, sample_size = 60)
fit <- lm(y ~ 1, data = df)
fit_1 <- lm(y ~ poly(x, degree = 1), data = df)
fit_2 <- lm(y ~ poly(x, degree = 2), data = df)
fit_many <- lm(y ~ poly(x, degree = 20), data = df)
truth <- seq(from = 0, to = 1, by = 0.01)
# Combine the data and model fits into a single data frame
df <- data.frame(
x = df$x,
y = df$y,
fit = predict(fit),
fit_1 = predict(fit_1),
fit_2 = predict(fit_2),
fit_many = predict(fit_many)
)
# Reshape the data frame into long format
df_long <- pivot_longer(
df,
cols = starts_with("fit_"),
names_to = "model",
values_to = "value"
) %>%
mutate(
model = case_when(
model == "fit" ~ "y = b",
model == "fit_1" ~ "y = b + mx",
model == "fit_2" ~ "y = b + mx + nx^2",
model == "fit_many" ~ "y = b + mx + nx^2 + ... + zx^20",
TRUE ~ model
)
)
# Plot
p <- ggplot(df_long, aes(x = x, y = value, color = model)) +
facet_wrap(~model, ncol = 2, scales = "free") +
geom_point(aes(y = y), alpha = .4, size = 2) +
geom_line(linewidth = .9, linetype = 1) +
scale_color_brewer(palette = "Set1") +
theme(legend.position = "none") +
geom_blank()
p