get_lin_least_sq_model: get_lin_least_sq_model

Description Usage Arguments Details Value Examples

View source: R/lin_least_squares_train_test.R View source: R/get_lin_least_sq_model.R

Description

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

Usage

1
get_lin_least_sq_model(x, y, intercept = TRUE, weighted = FALSE)

Arguments

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)

Details

works only with numeric data

Value

A list of objects, including calculations of the estimates for the beta coefficients for each predictor, along with residuals and model evaluation metrics

Examples

 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)

rguin26/HW4 documentation built on Dec. 22, 2021, 3:09 p.m.