predict_polynomial_regression.Rd
This function predicts the response variable using the fitted Polynomial Regression model.
predict_polynomial_regression(model, new_data, x_vars, degree = 2)
A list containing the fitted coefficients and the design matrix of the Polynomial Regression model
A data frame containing the new predictor variable values
A character vector of column names corresponding to the predictor variables
The degree of the polynomial (default = 2)
A vector of predicted response variable values
# Generate sample data
set.seed(123)
n <- 100
p <- 10
data <- data.frame(matrix(runif(n * p, -1, 1), n, p))
colnames(data) <- paste0("x", 1:p)
data$y <- data$x1^2 + data$x2^2 + rnorm(n, sd = 0.1)
# Fit the Polynomial Regression using the function
fit <- fit_polynomial_regression(data, x_vars=paste0("x", 1:p), y_var="y", degree=2)
# Generate new data
new_data <- data.frame(matrix(runif(10 * p, -1, 1), 10, p))
colnames(new_data) <- paste0("x", 1:p)
# Predict the response variable using the fitted model
predictions <- predict_polynomial_regression(fit, new_data, x_vars=paste0("x", 1:p), degree=2)
print(predictions)
#> [,1]
#> 1 0.6709962
#> 2 0.0695770
#> 3 0.4651418
#> 4 0.6782351
#> 5 0.1150972
#> 6 0.8299452
#> 7 0.8411522
#> 8 0.3750593
#> 9 1.0416450
#> 10 0.9291266