lm_resid_group: Fit linear regressions by group, with the option of removing...

View source: R/lm_resid_group.R

lm_resid_groupR Documentation

Fit linear regressions by group, with the option of removing outliers using a interactive plot of residuals.

Description

With this function it's possible to fit linear regressions by a grouping variable, and evaluate each equation via a interactive plot of residuals, and get a data frame. with each column as a coefficient and quality of fit variables, and other output options. Works with dplyr grouping functions.

Usage

lm_resid_group(
  df,
  model,
  .groups = NA,
  output_mode = "table",
  est.name = "est",
  keep_model = FALSE,
  rmoutliers = FALSE,
  fct_to_filter = NA,
  rmlevels = NA,
  onlyfiteddata = FALSE
)

Arguments

df

A data frame.

model

A linear regression model, with or without quotes. The variables mentioned in the model must exist in the provided data frame. X and Y sides of the model must be separated by "~".

.groups

Quoted name(s) of grouping variables used to fit multiple regressions, one for each level of the provided variable(s). Default NA.

output_mode

Selects different output options. Can be either "table", "merge", "merge_est" and "nest". See details for explanations for each option. Default: "table".

est.name

Name of the estimated y value. Used only if est.name = TRUE. Default: "est".

keep_model

If TRUE, a column containing lm object(s) is kept in the output. Useful if the user desires to get more information on the regression. Default: FALSE.

rmoutliers

If TRUE, outliers are filtered out using the IQR method. Default: FALSE.

fct_to_filter

Name of a factor or character column to be used as a filter to remove levels. Default: NA.

rmlevels

Levels of the fct_to_filter variable to be removed from the fit Default: NA.

onlyfiteddata

If TRUE, the output data will be the same as the fitted (and possibly filtered) data. Default: FALSE.

Details

this function uses lm_table as a basis, but calls a plot of residuals for each fitted model, for the user to evaluate. If one decides to remove any of the points, one can click and drag, and then click on the 'remove points' button. After that, one must simply click 'done' and the coefficients will be printed out.

It's possible to use the output argument to get a merged table if output="merge", that binds the original data frame and the fitted coefficients. If output="merge_est" we get a merged table as well, but with y estimated using the coefficients. If the fit is made using groups, this is taken into account, i.e. the estimation is made by group.

If output="nest", a data frame with nested columns is provided. This can be used if the user desires to get a customized output.

Value

A data frame. Different data frame options are available using the output argument.

Author(s)

Sollano Rabelo Braga sollanorb@gmail.com

Examples

if (interactive() ){
  library(forestmangr)
  library(dplyr)

  data("exfm19")

  # Fit SH model by group:
  lm_resid_group(exfm19, log(VWB) ~  log(DBH) + log(TH), "STRATA")

}


forestmangr documentation built on Sept. 15, 2024, 1:06 a.m.