Description Usage Arguments Details Value Examples
View source: R/lin_least_squares_train_test.R View source: R/get_lin_least_sq_model.R
Performs linear regression using ordinary least squares (OLS) or weighted least squares (WLS), with either an intercept included or not. This works with both simple and multiple lienar regression. It calculates and returns the beta coefficients of the least squares linear regression equation, residuals, as well as model evaluation metrics including sum of squared errors (SSE), mean squared error (MSE), root mean squared error (RMSE), mean absolute error (MAE), r-squared, and adjusted r-squared
1 | get_lin_least_sq_model(x, y, intercept = TRUE, weighted = FALSE)
|
x |
matrix, dataframe, or vector of all predictor/predictors and its/their respective values |
y |
vector of target values from the matrix of known predictor values |
intercept |
TRUE by default, it computes the beta coefficients with an intercept included; if it is set to FALSE, then no intercept is used |
weighted |
FALSE by default, it computes the beta coefficients using ordinary least squares (OLS); if it is set to TRUE, then the beta coefficients are calculated using weighted least squares (WLS) |
works only with numeric data
A list of objects, including calculations of the estimates for the beta coefficients for each predictor, along with residuals and model evaluation metrics
beta - vector of beta coefficients, with values corresponding to their respective column in x, and starting with (intercept) if intercept = TRUE
fitted_values - vector consisting of the fitted values of the data, x, used to build the model, calculated simply as the model's prediction value for each instance in x
residuals - vector consisting of the residuals of each instance in x, calculated as the fitted value minus the actual value for each instance in x
model_eval_metrics - vector consisting of evaluation metrics of the model, including sum of squared errors (SSE), mean squared error (MSE), root mean squared error (RMSE), mean absolute error (MAE), r-squared, and adjusted r-squared
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # Example 1
x <- sample(100, 30, replace=FALSE)
y <- sample(100, 30, replace=TRUE)
ols_result <- get_lin_least_sq_model(x, y)
wls_result <- get_lin_least_sq_model(x, y, weighted = TRUE)
ols_result_no_intercept <- get_lin_least_sq_model(x, y, intercept = FALSE)
wls_result_no_intercept <- get_lin_least_sq_model(x, y, intercept = FALSE, weighted = TRUE)
# Example 2
x <- matrix(sample(1000, 30*5, replace=FALSE), ncol = 5)
y <- sample(100, 30, replace=TRUE)
ols_result <- get_lin_least_sq_model(x, y)
wls_result <- get_lin_least_sq_model(x, y, weighted = TRUE)
ols_result_no_intercept <- get_lin_least_sq_model(x, y, intercept = FALSE)
wls_result_no_intercept <- get_lin_least_sq_model(x, y, intercept = FALSE, weighted = TRUE)
# Example 3
temp_data <- data.frame(
hours=c(1, 1, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 8),
age=c(3, 6, 7, 2, 4, 5, 6, 3, 4, 5, 2, 2, 4, 5, 9, 8),
weight=c(23, 12, 31, 24, 56, 23, 12, 23, 34, 35, 36, 14, 23, 11, 13, 56),
score=c(48, 78, 72, 70, 66, 92, 93, 75, 75, 80, 95, 97, 90, 96, 99, 99)
)
x <- temp_data
x$score <- NULL
y <- temp_data$score
ols_result <- get_lin_least_sq_model(x, y)
wls_result <- get_lin_least_sq_model(x, y, weighted = TRUE)
ols_result_no_intercept <- get_lin_least_sq_model(x, y, intercept = FALSE)
wls_result_no_intercept <- get_lin_least_sq_model(x, y, intercept = FALSE, weighted = TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.