Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
svmiller committed Jan 11, 2024
1 parent 7db574c commit 3b9b278
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 76 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# stevemisc 1.8.0

- `rd_plot()` now has an `na.rm = TRUE` argument quietly passed to the extraction of the standard deviation of the residuals. This ensures that missing values in the data don't result in missing residuals, which then result in no standard deviation of the residuals.
- `linloess_plot()` now has a `resid` argument that allows for comparison against the model's residuals on the *y*-axis rather than the default (the raw values of *y* on the *y*-axis).
- Assorted documentation fixes.

# stevemisc 1.7.0

Expand Down
96 changes: 67 additions & 29 deletions R/linloess_plot.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
#' Compare Linear Smoother to LOESS Smoother for Your OLS Model
#'
#' @description \code{linloess_plot()} provides a visual diagnostic of the linearity assumption of the OLS model.
#' Provided an OLS model fit by \code{lm()} in base R, the function extracts the model frame and creates a faceted
#' scatterplot. For each facet, a linear smoother and LOESS smoother are estimated over the points. Users who run
#' this function can assess just how much the linear smoother and LOESS smoother diverge. The more they diverge, the
#' more the user can determine how much the OLS model is a good fit as specified. The plot will also point to potential
#' outliers that may need further consideration.
#' @description \code{linloess_plot()} provides a visual diagnostic of the
#' linearity assumption of the OLS model. Provided an OLS model fit by
#' \code{lm()} in base R, the function extracts the model frame and creates a
#' faceted scatterplot. For each facet, a linear smoother and LOESS smoother
#' are estimated over the points. Users who run this function can assess just
#' how much the linear smoother and LOESS smoother diverge. The more they
#' diverge, the more the user can determine how much the OLS model is a good
#' fit as specified. The plot will also point to potential outliers that may
#' need further consideration.
#'
#' @details This function makes an implicit assumption that there is no variable in the regression
#' formula with the name ".y".
#' @details This function makes an implicit assumption that there is no variable
#' in the regression formula with the name ".y" or ".resid".
#'
#' It may be in your interest (for the sake of rudimentary diagnostic checks) to
#' disable the standard error bands for particularly ill-fitting linear models.
#'
#' @return \code{linloess_plot()} returns a faceted scatterplot as a \pkg{ggplot2} object. The linear smoother is in solid blue (with blue
#' standard error bands) and the LOESS smoother is a dashed black line (with gray/default standard error bands). You can add
#' cosmetic features to it after the fact. The function may spit warnings to you related to the LOESS smoother, depending your data. I think
#' these to be fine the extent to which this is really just a visual aid and an informal diagnostic for the linearity assumption.
#' @return \code{linloess_plot()} returns a faceted scatterplot as a
#' \pkg{ggplot2} object. The linear smoother is in solid blue (with blue
#' standard error bands) and the LOESS smoother is a dashed black line (with
#' gray/default standard error bands). You can add cosmetic features to it after
#' the fact. The function may spit warnings to you related to the LOESS smoother,
#' depending your data. I think these to be fine the extent to which this is
#' really just a visual aid and an informal diagnostic for the linearity
#' assumption.
#'
#' @author Steven V. Miller
#'
#' @param mod a fitted OLS model
#' @param se logical, defaults to \code{TRUE}. If \code{TRUE}, gives standard error estimates with the assorted smoothers.
#' @param ... optional parameters, passed to the scatterplot (\code{geom_point()}) component of this function. Useful if you want to make the smoothers more legible against the points.
#' @param resid logical, defaults to \code{FALSE}. If \code{FALSE}, the y-axis
#' on these plots are the raw values of the dependent variable. If \code{TRUE},
#' the y-axis is the model's residuals. Either work well here for the matter
#' at hand, provided you treat the output here as illustrative or suggestive.
#' @param se logical, defaults to \code{TRUE}. If \code{TRUE}, gives standard
#' error estimates with the assorted smoothers.
#' @param ... optional parameters, passed to the scatterplot
#' (\code{geom_point()}) component of this function. Useful if you want to make
#' the smoothers more legible against the points.
#'
#' @examples
#'
Expand All @@ -32,22 +46,46 @@
#' linloess_plot(M1, color="black", pch=21)


linloess_plot <- function(mod, se = TRUE, ...) {
linloess_plot <- function(mod, resid = FALSE, se = TRUE, ...) {
modframe <- model.frame(mod)

dat <- gather(modframe, "var", "value", 2:ncol(modframe))
colnames(dat)[1] <- c(".y")

ggplot(dat, aes(.data$value, .data$.y)) +
# Create your facet now since the var variable is the particular x variable.
# Make sure to set scale="free_x" because these x variables are all on different scales
ggplot2::facet_wrap(~var, scale="free_x") +
# scatterplot
geom_point(...) +
# linear smoother
geom_smooth(method="lm", fill="blue", se = se) +
# loess smoother, with different color
geom_smooth(method="loess", color="black", linetype="dashed",
se = se)
if(resid == FALSE) {

dat <- gather(modframe, "var", "value", 2:ncol(modframe))
colnames(dat)[1] <- c(".y")

ggplot(dat, aes(.data$value, .data$.y)) +
# Create your facet now since the var variable is the particular x variable.
# Make sure to set scale="free_x" because these x variables are all on different scales
ggplot2::facet_wrap(~var, scale="free_x") +
# scatterplot
geom_point(...) +
# linear smoother
geom_smooth(method="lm", fill="blue", se = se) +
# loess smoother, with different color
geom_smooth(method="loess", color="black", linetype="dashed",
se = se)

} else {
modframe$.resid <- resid(mod)
modframe <- modframe[c(".resid",names(modframe)[c(-1, -ncol(modframe))])]

dat <- gather(modframe, "var", "value", 2:ncol(modframe))
colnames(dat)[1] <- c(".resid")

ggplot(dat, aes(.data$value, .data$.resid)) +
# Create your facet now since the var variable is the particular x variable.
# Make sure to set scale="free_x" because these x variables are all on different scales
ggplot2::facet_wrap(~var, scale="free_x") +
# scatterplot
geom_point(...) +
# linear smoother
geom_smooth(method="lm", fill="blue", se = se) +
# loess smoother, with different color
geom_smooth(method="loess", color="black", linetype="dashed",
se = se)
}



}
2 changes: 1 addition & 1 deletion R/p_z.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#'
#' @param x a numeric vector (one or multiple) between 0 or 1
#' @param ts a logical, defaults to TRUE. If TRUE, returns two-sided critical z-value.
#' If FALSE, the function returns a one-sized critical z-value.
#' If FALSE, the function returns a one-sided critical z-value.
#'
#' @return This function takes a numeric vector, corresponding to the p-value you want, and
#' returns a numeric vector coinciding with the z-value you want under the standard normal
Expand Down
3 changes: 3 additions & 0 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pandoc: 3.1.1
pkgdown: 2.0.7
pkgdown_sha: ~
articles: {}
last_built: 2024-01-04T13:40Z
last_built: 2024-01-11T09:34Z
urls:
reference: http://svmiller.com/reference
article: http://svmiller.com/articles
Expand Down
Loading

0 comments on commit 3b9b278

Please sign in to comment.