multivariate-statistics/figures/plots/linear_regression.R

27 lines
707 B
R
Raw Normal View History

2023-11-16 16:47:14 +01:00
# Plot an affine model
n <- 250
sd <- 0.05
epsilon <- rnorm(n, mean = 0, sd = 2)
beta0 <- 1.25
beta1 <- 4
linear_model <- function(x) {
return(beta0 + beta1*x)
}
x <- runif(n, min=0, max=1)
y <- linear_model(x) + epsilon
pdf("figures/plots/linear_regression_linear.pdf")
plot(x, y, col="#5654fa", type="p", pch=20, xlab="x", ylab="y")
abline(a = beta0, b = beta1, col="red")
dev.off()
non_linear_model <- function(x) {
return(beta0 + beta1 * exp(2*x))
}
non_linear_y <- non_linear_model(x) + epsilon
pdf("figures/plots/linear_regression_non_linear.pdf")
plot(x, non_linear_y, col="#5654fa", type="p", pch=20, xlab="x", ylab="z")
curve(non_linear_model, from=0, to=1, add=T, col="red")
dev.off()