#Write a program to create an any application of Linear Regression in multivariate context for
predictive purpose.
# Sample data: Multivariate Linear Regression
# In this example, we'll predict "y" using multiple predictor variables "x1" and "x2".
# Generate some sample data
set.seed(123)
n <- 100 # Number of data points
x1 <- rnorm(n)
x2 <- rnorm(n)
error <- rnorm(n, mean = 0, sd = 0.5)
y <- 2 * x1 + 3 * x2 + error
# Implement multivariate linear regression manually
# Calculate means
mean_x1 <- mean(x1)
mean_x2 <- mean(x2)
mean_y <- mean(y)
# Calculate coefficients
beta_1 <- sum((x1 - mean_x1) * (y - mean_y)) / sum((x1 - mean_x1)^2)
beta_2 <- sum((x2 - mean_x2) * (y - mean_y)) / sum((x2 - mean_x2)^2)
beta_0 <- mean_y - beta_1 * mean_x1 - beta_2 * mean_x2
# Predict "y" based on the coefficients
y_predicted <- beta_0 + beta_1 * x1 + beta_2 * x2
# Calculate the residuals
residuals <- y - y_predicted
# Calculate the R-squared value
SSR <- sum((y_predicted - mean_y)^2)
SST <- sum((y - mean_y)^2)
r_squared <- SSR / SST
# Display the results
cat("Coefficient beta_0 (Intercept): ", beta_0, "\n")
cat("Coefficient beta_1 (x1): ", beta_1, "\n")
cat("Coefficient beta_2 (x2): ", beta_2, "\n")
cat("R-squared value: ", r_squared, "\n")