Skip to content

Commit

Permalink
Documented Standard Error code
Browse files Browse the repository at this point in the history
  • Loading branch information
user01 committed Nov 13, 2016
1 parent a0fe0b4 commit f0cb62b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ export(get_row)
export(if_else_)
export(is_invalid)
export(residuals_PRESS)
export(se)
export(set_colnames)
export(set_rownames)
export(vif_lm)
importFrom(car,vif)
importFrom(dplyr,"%>%")
importFrom(dplyr,first)
importFrom(dplyr,nth)
importFrom(purrr,"%@%")
importFrom(purrr,discard)
importFrom(purrr,map)
importFrom(purrr,map_chr)
Expand All @@ -26,4 +29,5 @@ importFrom(purrr,map_int)
importFrom(purrr,transpose)
importFrom(stats,lm)
importFrom(stats,lm.influence)
importFrom(stats,predict)
importFrom(stats,residuals)
25 changes: 25 additions & 0 deletions R/model_functions.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@

#' Standard Errors
#'
#' Returns the standard errors of the fit againt the data.
#' (y - yhat)^2
#' Without a supplied dataframe, returns the errors from the training fit
#'
#' @param fit Object of class "lm". A fitted model.
#' @param df DataFrame of predictors and predicted (optional)
#' @return double Squared differences of predicted and actual values
#' @examples
#' data.frame(y=1:5,x=c(1,2,2,4,5)) -> df1
#' data.frame(y=3:7,x=c(3,4,6,5,2)) -> df2
#' lm(y ~ ., df1) -> fit
#'
#' se(fit,df1)
#' se(fit,df2)
#' se(fit)
#'
#' @export
#' @importFrom dplyr %>%
#' @importFrom dplyr first
#' @importFrom purrr %@%
#' @importFrom stats predict
se <- function(fit, df = NULL) {
if (is.null(df)) {
# without a supplied to predict the fit, just use the attached values
Expand All @@ -9,12 +32,14 @@ se <- function(fit, df = NULL) {
return(simple_se)
}

# the first element of the terms is the regressor
fit %>%
get_("terms") %@%
"factors" %>%
rownames %>%
first -> regressor

# return the squared difference between predicted and actual value
fit %>%
predict(df) %>%
`-`(df[[regressor]]) %>%
Expand Down
32 changes: 32 additions & 0 deletions man/se.Rd

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

0 comments on commit f0cb62b

Please sign in to comment.