Movatterモバイル変換


[0]ホーム

URL:


Title:Veras Miscellaneous
Version:0.1.2
Description:Contains a collection of useful functions for basic data computation and manipulation, wrapper functions for generating 'ggplot2' graphics, including statistical model diagnostic plots, methods for computing statistical models quality measures (such as AIC, BIC, r squared, root mean squared error) and general utilities.
License:MIT + file LICENSE
URL:https://lveras.com/lvmisc/
BugReports:https://github.com/verasls/lvmisc/issues
Imports:cowplot, dplyr (≥ 1.0.0), ggplot2, glue, grDevices, methods,purrr, rlang (≥ 0.4.6), rsample, stats, tibble, tidyselect,vctrs (≥ 0.3.0)
Suggests:covr, devtools, forcats, fs, git2r, knitr, lme4, lmerTest,rmarkdown, testthat, usethis, vdiffr, withr
Encoding:UTF-8
RoxygenNote:7.2.3
VignetteBuilder:knitr
Config/testthat/edition:3
NeedsCompilation:no
Packaged:2024-01-25 22:15:35 UTC; lucasveras
Author:Lucas VerasORCID iD [aut, cre]
Maintainer:Lucas Veras <lucasdsveras@gmail.com>
Repository:CRAN
Date/Publication:2024-01-25 22:50:06 UTC

Abort based on issues with function argument

Description

Create a custom error condition created withrlang::abort() with a - hopefully - more usefulerror message and metadata.

Usage

abort_argument_type(arg, must, not)abort_argument_class(arg, must, not)abort_argument_length(arg, must, not)abort_argument_diff_length(arg1, arg2)abort_argument_value(arg, valid_values)

Arguments

arg

A character string with the argument name.

must

A character string specifying a condition the argument mustfulfill.

not

Either a character string specifying a condition the argumentmust not fulfill or the bare (unquoted) argument name. In the last case,the function evaluates the argument type (abort_argument_type()) orlength (abort_argument_length()) and displays the result in theerror message.

arg1,arg2

A character string with the argument name.

valid_values

A character vector with the valid values.

Value

Each function returns a classed error condition.abort_argument_type() returns aerror_argument_type class,abort_argument_length() returns aerror_argument_lengthclass,abort_argument_diff_length() returns aerror_argument_diff_length class andabort_argument_value()returns aerror_argument_value class.

See Also

abort_column_not_found(),abort_no_method_for_class()


Abort based on column not being found in a data frame

Description

Creates a custom error condition created withrlang::abort() with a - hopefully - more usefulerror message and metadata.

Usage

abort_column_not_found(data, col_name)

Arguments

data

A data frame.

col_name

A character vector with the column name.

Value

Returns an error condition of classerror_column_not_found.

See Also

abort_argument_type(),abort_argument_class(),abort_argument_length(),abort_argument_diff_length(),abort_no_method_for_class(),abort_package_not_installed()


Abort method if class is not implemented

Description

Creates a custom error condition created withrlang::abort() with a - hopefully - more usefulerror message and metadata.

Usage

abort_no_method_for_class(fun, class, ...)

Arguments

fun

A character vector with the function name.

class

A character vector with the class name.

...

Extra message to be added to the error message. Must becharacter string.

Value

Returns an error condition of classerror_no_method_for_class.

See Also

abort_argument_type(),abort_argument_class(),abort_argument_length(),abort_argument_diff_length(),abort_column_not_found(),abort_package_not_installed()


Abort if required package is not installed

Description

Creates a custom error condition created withrlang::abort() with a - hopefully - more usefulerror message and metadata.

Usage

abort_package_not_installed(package)

Arguments

package

A character string with the required package name.

Value

Returns an error condition of classerror_package_not_installed.

See Also

abort_argument_type(),abort_argument_class(),abort_argument_length(),abort_argument_diff_length(),abort_column_not_found(),abort_no_method_for_class()


Model accuracy

Description

Computes some common model accuracy indices, such as the R squared, meanabsolute error, mean absolute percent error and root mean square error.

Usage

accuracy(model, na.rm = FALSE)## Default S3 method:accuracy(model, na.rm = FALSE)## S3 method for class 'lvmisc_cv'accuracy(model, na.rm = FALSE)## S3 method for class 'lm'accuracy(model, na.rm = FALSE)## S3 method for class 'lmerMod'accuracy(model, na.rm = FALSE)

Arguments

model

An object of classlvmisc_cv or an object containinga model.

na.rm

A logical value indicating whether or not to stripNAvalues to compute the indices. Defaults toFALSE.

Details

The method for thelm class (or for thelvmisc_cvclass of alm) returns a data frame with the columnsAIC(Akaike information criterion),BIC (Bayesian informationcriterion),R2 (R squared),R2_adj (adjusted R squared),MAE (mean absolute error),MAPE (mean absolute percenterror) andRMSE (root mean square error).

The method for thelmerMod (or for thelvmisc_cv class of almerMod) returns a data frame with the columnsR2_marg andR2_cond instead of the columnsR2 andR2_adj.All the other columns are the same as the method forlm.R2_marg is the marginal R squared, which considers only the varianceby the fixed effects of a mixed model, andR2_cond is theconditional R squared, which considers both fixed and random effectsvariance.

Value

An object of classlvmisc_accuracy. See "Details" for moreinformation.

Examples

mtcars <- tibble::as_tibble(mtcars, rownames = "car")m <- stats::lm(disp ~ mpg, mtcars)cv <- loo_cv(m, mtcars, car, keep = "used")accuracy(m)accuracy(cv)

Params for the accuracy indices summary functions

Description

Params for the accuracy indices summary functions

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.


Params for the accuracy indices functions

Description

Params for the accuracy indices functions

Arguments

actual

A numeric vector with the actual values

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.


Bias

Description

Computes the bias (mean error) between the input vectors.

Usage

bias(actual, predicted, na.rm = FALSE)

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

A double scalar with the bias value.

See Also

mean_error(),loa()

Examples

actual <- runif(10)predicted <- runif(10)bias(actual, predicted)

Compute body mass index (BMI)

Description

bmi calculates the BMI in kilograms per meter squared.

Usage

bmi(mass, height)

Arguments

mass,height

A numerical vector with body mass and height data.massunit must be kilograms andheight unit must be meters. If theheightunit is centimeters, it is converted to meters before BMI computation anda warning is shown.

Value

Returns a double vector with the element-wise body mass index (BMI).

See Also

bmi_cat()

Examples

mass <- sample(50:100, 20)height <- rnorm(20, mean = 1.7, sd = 0.2)bmi(mass, height)

Classify body mass index (BMI) category

Description

bmi_cat returns the element-wise BMI category as factor with 6 levels:

Usage

bmi_cat(bmi)

Arguments

bmi

A numeric vector with BMI data.BMI unit must be meters persquare meter.

Value

A vector of classfactor with 6 levels: "Underweight","Normal weight", "Overweight", "Obesity class I", "Obesity class II"and "Obesity class III".

See Also

bmi()

Examples

mass <- sample(50:100, 20)height <- rnorm(20, mean = 1.7, sd = 0.2)bmi <- bmi(mass, height)bmi_cat(bmi)

Center variable

Description

Center a variable by subtracting the mean from each element. Centering canbe performed by the grand mean whenby = NULL (the default), or bygroup means whenby is a factor variable.

Usage

center_variable(variable, scale = FALSE, by = NULL)

Arguments

variable

A numeric vector.

scale

A logical vector. Ifscale = TRUE, the centered valuesofvariable are divided by their standard deviation.

by

A vector with thefactor class.

Value

A numeric vector.

Examples

df <- data.frame(  id = 1:20,  group = as.factor(sample(c("A", "B"), 20, replace = TRUE)),  body_mass = rnorm(20, mean = 65, sd = 12))df$body_mass_centered <- center_variable(df$body_mass, by = df$group)df

Checks whether a package is installed

Description

Checks whether a package is installed

Usage

check_package(x)

Arguments

x

A character string with the package name

Value

If all packages inx are installed, returnsTRUE,if not, returns the name of the non-installed package(s).


Clear the console

Description

Clear the console by printing 50 times the new line character ("\n").

Usage

cl()

Value

Prints to console. Called by its side-effects.


Clean observations

Description

Replace valid observations byNAs when a given subject has more thenmax_na missing values.

Usage

clean_observations(data, id, var, max_na)

Arguments

data

A data frame, or data frame extension (e.g. a tibble).

id

The bare (unquoted) name of the column that identifies eachsubject.

var

The bare (unquoted) name of the column to be cleaned.

max_na

An integer indicating the maximum number ofNAs persubject.

Value

The originaldata with thevar observations matchingthemax_na criterion replaced byNA.

Examples

set.seed(10)data <- data.frame(  id = rep(1:5, each = 4),  time = rep(1:4, 5),  score = sample(c(1:5, rep(NA, 2)), 20, replace = TRUE))clean_observations(data, id, score, 1)

Compare models accuracy

Description

Computes some common model accuracy indices of several different models atonce, allowing model comparison.

Usage

compare_accuracy(..., rank_by = NULL, quiet = FALSE)

Arguments

...

A list of models. The models can be of the same or of differentclasses, includinglvmisc_cv class.

rank_by

A character string with the name of an accuracy index to rankthe models by.

quiet

A logical indicating whether or not to show any warnings. IfFALSE (the default) no warnings are shown.

Value

Adata.frame with a model per row and an index per column.

Examples

m1 <- lm(Sepal.Length ~ Species, data = iris)m2 <- lm(Sepal.Length ~ Species + Petal.Length, data = iris)m3 <- lm(Sepal.Length ~ Species * Petal.Length, data = iris)compare_accuracy(m1, m2, m3)if (require(lme4, quietly = TRUE)) {  mtcars <- tibble::as_tibble(mtcars, rownames = "cars")  m1 <- lm(Sepal.Length ~ Species, data = iris)  m2 <- lmer(    Sepal.Length ~ Sepal.Width + Petal.Length + (1 | Species), data = iris  )  m3 <- lm(disp ~ mpg * hp, mtcars)  cv3 <- loo_cv(m3, mtcars, cars)  compare_accuracy(m1, m2, cv3, rank_by = "AIC")}

Create a project

Description

Creates a project structure, including sub-directories, and initializationof a git repository.

Usage

create_proj(  path,  sub_dirs = "default",  use_git = TRUE,  use_gitignore = "default",  use_readme = TRUE)

Arguments

path

A path to a directory that does not exist.

sub_dirs

A character vector. Ifsub_dirs = "default", itcreates 'code/', 'data/', 'docs/', 'figures/' and 'tables/'sub-directories. Otherwise, it creates the sub-directories specifiedin the character vector.

use_git

A logical value indicating whether or not to initialize a gitrepository. Defaults toTRUE.

use_gitignore

A character vector. Ifuse_gitignore = "default",it adds a .gitignore file with the files generated by your operatingsystem and by R, as well as some common file extensions. The default.gitignore is as generated bygitignore.io. Tocreate a custom .gitignore, add the files to be ignored in a charactervector. If you do not want to create a .gitignore file, setuse_gitignore = NULL.

use_readme

A logical value. IfTRUE (default), adds an empty'README.md' file.

Value

Path to the newly created project, invisibly.


Divide variable based on quantiles

Description

Creates a factor based on equally spaced quantiles of a variable.

Usage

divide_by_quantile(data, n, na.rm = TRUE)

Arguments

data

A numeric vector.

n

An integer specifying the number of levels in the factor to becreated.

na.rm

A logical vector indicating whether theNA values shouldbe removed before the quantiles are computed.

Value

A vector of classfactor indicating in which quantile theelement indata belongs.

See Also

stats::quantile().

Examples

x <- c(sample(1:20, 9), NA)divide_by_quantile(x, 3)

Error

Description

Computes the element-wise error between the input vectors.

Usage

error(actual, predicted)

Arguments

actual

A numeric vector with the actual values

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

Value

Returns a double vector with the element-wise error values.

See Also

error_pct(),error_abs(),error_abs_pct(),error_sqr().

Examples

actual <- runif(10)predicted <- runif(10)error(actual, predicted)

Absolute error

Description

Computes the element-wise absolute errors between the input vectors.

Usage

error_abs(actual, predicted)

Arguments

actual

A numeric vector with the actual values

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

Value

Returns a double vector with the element-wise absolute error values.

See Also

error(),error_pct(),error_abs_pct(),error_sqr().

Examples

actual <- runif(10)predicted <- runif(10)error_abs(actual, predicted)

Absolute percent error

Description

Computes the element-wise absolute percent errors between the input vectors.

Usage

error_abs_pct(actual, predicted)

Arguments

actual

A numeric vector with the actual values

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

Value

Returns a double vector with the element-wise absolute percenterror values.

A vector of the classlvmisc_percent with the element-wiseabsolute percent error values.

See Also

error(),error_pct(),error_abs(),error_sqr().

Examples

actual <- runif(10)predicted <- runif(10)error_abs_pct(actual, predicted)

Percent error

Description

Computes the element-wise percent error between the input vectors.

Usage

error_pct(actual, predicted)

Arguments

actual

A numeric vector with the actual values

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

Value

Returns a double vector with the element-wise percent error values.

A vector of the classlvmisc_percent with the element-wisepercent error values.

See Also

error(),error_abs(),error_abs_pct(),error_sqr().

Examples

actual <- runif(10)predicted <- runif(10)error_pct(actual, predicted)

Squared error

Description

Computes the element-wise squared errors between the input vectors.

Usage

error_sqr(actual, predicted)

Arguments

actual

A numeric vector with the actual values

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

Value

Returns a double vector with the element-wise squared error values.

See Also

error(),error_pct(),error_abs(),error_abs_pct().

Examples

actual <- runif(10)predicted <- runif(10)error_sqr(actual, predicted)

Extract information from the trained models from a cross-validation

Description

Extract information from the trained models from a cross-validation

Usage

get_cv_fixed_eff(cv)get_cv_r2(cv)

Arguments

cv

An object of classlvmisc_cv.

Value

get_cv_fixed_eff() returns a tibble with the estimatedvalue for each coefficient of each trained model and its associatedstandard error.get_cv_r2() returns a tibble with the R squaredfor each of the trained models.


Check whether value is outlier

Description

is_outlier returns a logical vector indicating whether a value is anoutlier based on the rule of 1.5 times the interquartile range above thethird quartile or below the first quartile.

Usage

is_outlier(x, na.rm = FALSE)

Arguments

x

A numerical vector

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

A logical vector.

See Also

stats::IQR(),stats::quantile()

Examples

x <- c(1:8, NA, 15)is_outlier(x, na.rm = TRUE)

Limits of agreement

Description

Computes the Bland-Altman limits of agreement between the input vectors.

Usage

loa(actual, predicted, na.rm = FALSE)

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

A named list with the lower and upper limits of agreement values,respectively.

See Also

mean_error(),bias()

Examples

actual <- runif(10)predicted <- runif(10)loa(actual, predicted)

Leave-one-out cross-validation

Description

Cross-validates the model using the leave-one-out approach. In this methodeach subject's data is separated into a testing data set, and all othersubject's are kept in the training data set, with as many resamples asthe number of subjects in the original data set. It computes the model'spredicted value in the testing data set for each subject.

Usage

loo_cv(model, data, id, keep = "all")## Default S3 method:loo_cv(model, data, id, keep = "all")## S3 method for class 'lm'loo_cv(model, data, id, keep = "all")## S3 method for class 'lmerMod'loo_cv(model, data, id, keep = "all")

Arguments

model

An object containing a model.

data

A data frame.

id

The bare (unquoted) name of the column which identifies subjects.

keep

A character string which controls which columns are present inthe output. Can be one of three options:

  • "all": The default. Retain all variables in the original data frameplus the".actual" and".predicted" columns.

  • "used": Keeps only the"id" column of the original data frame, plusthe".actual" and".predicted" columns.

  • "none": Returns just the".actual" and '".predicted" columns.

Value

Returns an object of classlvmisc_cv. A tibble containing the".actual" and".predicted" columns.

Examples

mtcars$car <- row.names(mtcars)m <- stats::lm(disp ~ mpg, mtcars)loo_cv(m, mtcars, car, keep = "used")

Last error

Description

lt() prints the last error and the full backtrace andle()returns the last error with a simplified backtrace. These functions arejust wrappers torlang::last_trace() andrlang::last_error() respectively.

Usage

lt()le()

Value

An object of classrlang_trace.

An object of classrlang_error.


Number of elements in a vector.

Description

lunique returns the number of non-NA unique elements andlnareturns the number ofNAs.

Usage

lunique(x)lna(x)

Arguments

x

A vector.

Value

A non-negative integer.

See Also

length(),unique(),is.na()

Examples

x <- sample(c(1:3, NA), 10, replace = TRUE)lunique(x)lna(x)

Internal vctrs methods

Description

Internal vctrs methods


Mean error

Description

Computes the average error between the input vectors.

Usage

mean_error(actual, predicted, na.rm = FALSE)

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

Returns a double scalar with the mean error value.

See Also

mean_error_pct(),mean_error_abs(),mean_error_abs_pct(),mean_error_sqr(),mean_error_sqr_root()

Examples

actual <- runif(10)predicted <- runif(10)mean_error(actual, predicted)

Mean absolute error

Description

Computes the average absolute error between the input vectors.

Usage

mean_error_abs(actual, predicted, na.rm = FALSE)

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

Returns a double scalar with the mean absolute error value.

See Also

mean_error(),mean_error_pct(),mean_error_abs_pct(),mean_error_sqr(),mean_error_sqr_root()

Examples

actual <- runif(10)predicted <- runif(10)mean_error_abs(actual, predicted)

Mean absolute percent error

Description

Computes the average absolute percent error between the input vectors.

Usage

mean_error_abs_pct(actual, predicted, na.rm = FALSE)

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

Returns a double scalar with the mean absolute percent error value.

A vector of the classlvmisc_percent.

See Also

mean_error(),mean_error_abs(),mean_error_pct(),mean_error_sqr(),mean_error_sqr_root()

Examples

actual <- runif(10)predicted <- runif(10)mean_error_abs_pct(actual, predicted)

Mean percent error

Description

Computes the average percent error between the input vectors.

Usage

mean_error_pct(actual, predicted, na.rm = FALSE)

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

Returns a double scalar with the mean percent error value.

A vector of the classlvmisc_percent.

See Also

mean_error(),mean_error_abs(),mean_error_abs_pct(),mean_error_sqr(),mean_error_sqr_root()

Examples

actual <- runif(10)predicted <- runif(10)mean_error_pct(actual, predicted)

Mean square error

Description

Computes the average square error between the input vectors.

Usage

mean_error_sqr(actual, predicted, na.rm = FALSE)

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

Returns a double scalar with the mean square error value.

See Also

mean_error(),mean_error_abs(),mean_error_pct(),mean_error_abs_pct(),mean_error_sqr_root()

Examples

actual <- runif(10)predicted <- runif(10)mean_error_sqr(actual, predicted)

Root mean square error

Description

Computes the root mean square error between the input vectors.

Usage

mean_error_sqr_root(actual, predicted, na.rm = FALSE)

Arguments

actual

A numeric vector with the actual values.

predicted

A numeric vector with the predicted values. Each element inthis vector must be a prediction for the corresponding element inactual.

na.rm

A logical value indicating whetherNA values should bestripped before the computation proceeds. Defaults toFALSE.

Value

Returns a double scalar with the root mean square error value.

See Also

mean_error(),mean_error_abs(),mean_error_pct(),mean_error_abs_pct(),mean_error_sqr()

Examples

actual <- runif(10)predicted <- runif(10)mean_error_sqr_root(actual, predicted)

Constructor for lvmisc_accuracy object

Description

Constructor for lvmisc_accuracy object

Usage

new_lvmisc_accuracy(accuracy_data, model_class)

Arguments

accuracy_data

A data frame with accuracy indices.

model_class

The class of the model.


Constructor for lvmisc_cv object

Description

Constructor for lvmisc_cv object

Usage

new_lvmisc_cv(x, model, trained_models)

Arguments

x

A data.frame.

model

A list of all trained models.


Value matching

Description

Value matching

Usage

x %!in% table

Arguments

x

Vector with the values to be matched.

table

Vector with the values to be matched against.

Value

A logical vector indicating which values are not intable.

See Also

match().

Examples

x <- 8:12x %!in% 1:10

Print all rows of a data frame or tibble

Description

Shortcut to print all rows of a data frame or tibble. Useful to inspect thewhole tibble, as it prints by default only the first 20 rows.

Usage

pa(data)

Arguments

data

A data frame or tibble.

Value

Printsdata and returns it invisibly.

See Also

print() andprinting tibbles.

Examples

df <- dplyr::starwarspa(df)

percent vector

Description

Creates a double vector that represents percentages. When printed, it ismultiplied by 100 and suffixed with⁠%⁠.

Usage

percent(x = double())is_percent(x)as_percent(x)

Arguments

x
  • Forpercent(): A numeric vector

  • Foris_percent(): An object to test.

  • Foras_percent(): An object to cast.

Value

An S3 vector of classlvmisc_percent.

Examples

percent(c(0.25, 0.5, 0.75))

Computes the percent change

Description

percent_change returns the element-wise percent change between twonumeric vectors.

Usage

percent_change(baseline, followup)

Arguments

baseline,followup

A numeric vector with data to compute the percentchange.

Value

A vector of classlvmisc_percent.

See Also

percent(),{error_pct()}

Examples

baseline <- sample(20:40, 10)followup <- baseline * runif(10, min = 0.5, max = 1.5)percent_change(baseline, followup)

Create a Bland-Altman plot

Description

Create a Bland-Altman plot as described by Bland & Altman (1986).

Usage

plot_bland_altman(x, ...)

Arguments

x

An object of classlvmisc_cv or an object containing a model.

...

Additional arguments to be passed toggplot2::aes().

Value

Aggplot object.

References

Examples

mtcars <- tibble::as_tibble(mtcars, rownames = "car")m <- stats::lm(disp ~ mpg, mtcars)cv <- loo_cv(m, mtcars, car)plot_bland_altman(cv, colour = as.factor(am))

Plot model diagnostics

Description

Plotting functions for some common model diagnostics.

Usage

plot_model(model)plot_model_residual_fitted(model)plot_model_scale_location(model)plot_model_qq(model)plot_model_cooks_distance(model)plot_model_multicollinearity(model)

Arguments

model

An object containing a model.

Details

plot_model_residual_fitted() plots the model residualsversus the fitted values.plot_model_scale_location() plots thesquare root of absolute value of the model residuals versus the fittedvalues.plot_model_qq() plots a QQ plot of the model standardizedresiduals.plot_model_cooks_distance() plots a bat chart of eachobservation Cook's distance value.plot_model_multicollinearity()plots a bar chart of the variance inflation factor (VIF) for each of themodel terms.plot_model() returns a plot grid with all theapplicable plot diagnostics to a given model.

Value

Aggplot object.

Examples

m <- lm(disp ~ mpg + hp + cyl + mpg:cyl, mtcars)plot_model(m)plot_model_residual_fitted(m)plot_model_scale_location(m)plot_model_qq(m)plot_model_cooks_distance(m)plot_model_multicollinearity(m)

Quick plotting

Description

These functions are intended to be used to quickly generate simpleexploratory plots using the packageggplot2.

Usage

plot_scatter(data, x, y, ...)plot_line(data, x, y, ...)plot_hist(data, x, bin_width = NULL, ...)plot_qq(data, x, ...)

Arguments

data

A data frame.

x,y

x and y aesthetics as the bare (unquoted) name of a column indata.

...

Additional arguments to be passed to theggplot2::aes()function.

bin_width

The width of the bins in a histogram. WhenNULL(default), it uses the number of bins inbins (defaults to 30).You can also use one of the character strings"Sturges","scott" or"FD" to use one of the methods to determine thebin width as ingrDevices::nclass.*()

Value

Aggplot object.

Examples

plot_scatter(mtcars, disp, mpg, color = factor(cyl))plot_line(Orange, age, circumference, colour = Tree)plot_hist(iris, Petal.Width, bin_width = "FD")plot_qq(mtcars, mpg)

Compute R squared

Description

Returns the R squared values according to the model class.

Usage

r2(model)## Default S3 method:r2(model)## S3 method for class 'lm'r2(model)## S3 method for class 'lmerMod'r2(model)

Arguments

model

An object containing a model.

Details

R squared computations.

Value

If the model is a linear model, it returns adata.framewith the R squared and adjusted R squared values. If the model is alinear mixed model it return adata.frame with the marginal andconditional R squared values as described by Nakagawa and Schielzeth(2013). See the formulas for the computations in "Details".

R squared

R^2 = \frac{var(\hat{y})}{var(\epsilon)}

Wherevar(\hat{y}) is the variance explained by the model andvar(\epsilon) is the residual variance.

Adjusted R squared

R_{adj}^{2} = 1 - (1 - R^2)\frac{n - 1}{n - p - 1}

Wheren is the number of data points andp is the number ofpredictors in the model.

Marginal R squared

R_{marg}^{2} = \frac{var(f)}{var(f) + var(r) + var(\epsilon)}

Wherevar(f) is the variance of the fixed effects,var(r) isthe variance of the random effects andvar(\epsilon) is theresidual variance.

Conditional R squared

R_{cond}^{2} = \frac{var(f) + var(r)}{var(f) + var(r) + var(\epsilon)}

References

Examples

m1 <- lm(Sepal.Length ~ Species, data = iris)r2(m1)if (require(lme4, quietly = TRUE)) {  m2 <- lmer(    Sepal.Length ~ Sepal.Width + Petal.Length + (1 | Species), data = iris  )  r2(m2)}

Repeat baseline levels

Description

Returns a vector with the length equal to the number of rows in thedata with the baseline value of thevar repeated for everytime value of eachid.

Usage

repeat_baseline_values(data, var, id, time, baseline_level, repeat_NA = TRUE)

Arguments

data

A data frame.

var

The bare (unquoted) name of the column with the values to berepeated.

id

The bare (unquoted) name of the column that identifies eachsubject.

time

The bare (unquoted) name of the column with the time values.

baseline_level

The value oftime corresponding the baseline.

repeat_NA

A logical vector indicating whether or notNA valuesin thevar will correspond toNA values in return vector.Defaults toTRUE.

Value

A vector of the same lenght and class ofvar.

Examples

df <- data.frame( id = rep(1:5, each = 4), time = rep(1:4, 5), score = rnorm(20, mean = 10, sd = 2))df$baseline_score <- repeat_baseline_values(df, score, id, time, 1)df

Capture a backtrace

Description

Captures the sequence of calls that lead to the current function. It is justa wrapper torlang::trace_back().

Usage

tb(...)

Arguments

...

Passed torlang::trace_back().

Value

An object of classrlang_trace.


Variance inflation factor

Description

Computes the variance inflation factor (VIF). The VIF is a measure of howmuch the variance of a regression coefficient is increased due tocollinearity.

Usage

vif(model)## Default S3 method:vif(model)## S3 method for class 'lm'vif(model)## S3 method for class 'lmerMod'vif(model)

Arguments

model

An object containing a model.

Details

VIF interpretation

As a rule of thumb for the interpretation of the VIF value, a VIFless than 5 indicates a low correlation of a given model term with theothers, a VIF between 5 and 10 indicates a moderate correlation and aVIF greater than 10 indicates a high correlation.

Value

It returns adata.frame with three columns: the name of themodel term, the VIF value and its classification (see "Details").

References

Examples

m <- lm(disp ~ mpg + cyl + mpg:cyl, mtcars)vif(m)

[8]ページ先頭

©2009-2025 Movatter.jp