Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
svmiller committed Nov 6, 2023
1 parent 5bf1d00 commit 5019349
Show file tree
Hide file tree
Showing 94 changed files with 325 additions and 67 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: stevemisc
Type: Package
Title: Steve's Miscellaneous Functions
Version: 1.6.9
Version: 1.7.0
Depends: R (>= 3.6.0), stats
Authors@R: c(
person("Steve", "Miller", email = "[email protected]",
Expand Down
5 changes: 3 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ importFrom(stats, "nobs")
importFrom("stats", "complete.cases", "update", "rexp")
importFrom(tidyr, "unite")

# This is for wls()
# This is for wls() and rd_plot()

importFrom("stats", "lm", "na.exclude", "resid")

importFrom("stats", "lm", "na.exclude")
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# stevemisc 1.7.0

- Add `charitable_contributions`.
- Add `rd_plot()`
- Scoped helper verbs (the "at" functions) are gradually getting `.by` support and, with it, breaking their link to the superseded `_at()` functions from `{dplyr}`.
- `linloess_plot()` now has a `se` argument for optionally disabling standard error bands. For particularly ill-fitting linear models, this may be advisable.

# stevemisc 1.6.0

Expand Down
11 changes: 8 additions & 3 deletions R/linloess_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#' @details This function makes an implicit assumption that there is no variable in the regression
#' formula with the name ".y".
#'
#' 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
Expand All @@ -18,6 +21,7 @@
#' @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.
#'
#' @examples
Expand All @@ -28,7 +32,7 @@
#' linloess_plot(M1, color="black", pch=21)


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

dat <- gather(modframe, "var", "value", 2:ncol(modframe))
Expand All @@ -41,8 +45,9 @@ linloess_plot <- function(mod, ...) {
# scatterplot
geom_point(...) +
# linear smoother
geom_smooth(method="lm", fill="blue") +
geom_smooth(method="lm", fill="blue", se = se) +
# loess smoother, with different color
geom_smooth(method="loess", color="black", linetype="dashed")
geom_smooth(method="loess", color="black", linetype="dashed",
se = se)

}
48 changes: 48 additions & 0 deletions R/rd_plot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#' Residual Density Plot for Linear Models
#'
#' @description \code{rd_plot()} provides a visual diagnostic of the normality
#' assumption of the linear model. Provided an OLS model fit by \code{lm()} in
#' base R, the function extracts the residuals of the model and creates
#' a density plot of those residuals (solid black line) against a standard
#' normal distribution with a mean of 0 and a standard deviation matching the
#' standard deviation of the residuals from the model. The function may be used
#' for diagnostic purposes.
#'
#' @details The user can always add \pkg{ggplot2} elements on top of this for
#' greater legibility/clarity. For example, density plots can be finicky about
#' making observations appear where they don't. Perhaps adjusting the scale
#' of \code{x} ad hoc, after the fact, may be warranted.
#'
#' The goal of this function is to emphasize that in many real world applications,
#' the normality assumption of the residuals is never held but can often be
#' reasonably approximated upon visual inspection.
#'
#' @return \code{rd_plot()} returns a density plot a \pkg{ggplot2} object. A
#' density plot of the actual residuals is a solid black line. A stylized normal
#' distribution matching the description of the residuals is the blue dashed
#' line.
#'
#' @author Steven V. Miller
#'
#' @param mod a fitted linear model
#'
#' @examples
#'
#' M1 <- lm(mpg ~ ., data=mtcars)
#'
#' rd_plot(M1)


rd_plot <- function(mod) {

sdr <- sd(resid(mod))

hold_this <- data.frame(x = resid(mod))

ggplot(hold_this, aes(x)) +
geom_density() +
stat_function(fun = dnorm, color="blue",
args = list(mean = 0, sd = sdr),
linetype="dashed", linewidth=1.1)

}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ reference:
- r1sd
- r2sd
- rbnorm
- rd_plot
- revcode
- sbayesboot
- sbtscs
Expand Down
2 changes: 1 addition & 1 deletion docs/404.html

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

6 changes: 3 additions & 3 deletions docs/authors.html

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

4 changes: 2 additions & 2 deletions docs/deps/bootstrap-5.2.2/bootstrap.min.css

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified docs/deps/bootstrap-5.2.2/fonts/CSR64z1Qlv-GDxkbKVQ_fOAKSw.woff
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/index.html

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

6 changes: 5 additions & 1 deletion docs/news/index.html

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

4 changes: 2 additions & 2 deletions docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pandoc: 2.19.2
pandoc: 3.1.1
pkgdown: 2.0.7
pkgdown_sha: ~
articles: {}
last_built: 2023-05-05T13:01Z
last_built: 2023-11-06T08:18Z
urls:
reference: http://svmiller.com/reference
article: http://svmiller.com/articles
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/at.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/reference/binred_plot.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/reference/carrec.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/reference/charitable_contributions.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/reference/cor2data.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/reference/corvectors.html

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

6 changes: 3 additions & 3 deletions docs/reference/db_lselect.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/reference/ess9_labelled.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/reference/fct_reorg.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/reference/filter_refs.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/reference/fra_leaderyears.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/reference/get_sims.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/reference/get_var_info.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/reference/ggplot-themes.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/reference/gmy_dyadyears.html

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

Loading

0 comments on commit 5019349

Please sign in to comment.