fit_reg: Fit OLS or GLM on the data

Description Usage Arguments Details Value Examples

View source: R/boot-empirical.R

Description

fit_reg fits an OLS or GLM on the input dataset.

Usage

1
fit_reg(mod_fit, data, weights = NULL)

Arguments

mod_fit

An object of class lm or glm to fit on the data. This object should contain the formula, the data, and, in case of glm, the glm family.

data

A tibble or data frame containing the dataset on which the model will be trained.

weights

A character corresponding to the name of the weights feature name in the data.

Details

The model specification obtained from mod_fit, of class lm or glm, is fitted on data. The user can choose to fit a weighted regression using the argument weights.

Value

A tibble containing the estimated coefficients (term) of the regressors (term).

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
31
## Not run: 
set.seed(457468)
# Estimate ols from a bootstraped dataset
n <- 1e3
X <- stats::rnorm(n, 0, 1)
y <- 2 + X * 1 + stats::rnorm(n, 0, 1)
lm_fit <- stats::lm(y ~ X)
boot <- comp_boot_emp_samples(model.frame(lm_fit))$data[[1]]
mod <- fit_reg(lm_fit, boot)

# Display the output
print(mod)

#' # Estimate OLS from a dataset with weights
n <- 1e3
X <- stats::rnorm(n, 0, 1)
y <- 2 + X * 1 + stats::rnorm(n, 0, 1)
mod_fit <- lm(y ~ X)

# fit weighted regression
reg_df <- tibble::tibble(y = y, X = X, weights = 1:length(X))
mod <- fit_reg(mod_fit, reg_df, "weights")
print(mod)

# fit unweighted regression
mod <- fit_reg(mod_fit, reg_df)
print(mod)
# compare this output with the output from lm
coef(lm(y ~ X, reg_df))

## End(Not run)

shamindras/maar documentation built on Sept. 19, 2021, 10:21 p.m.