modelr: Modelling Functions that Work with the Pipe

パイプ処理に適したモデリング関数

> library(modelr)

バージョン: 0.1.0


関数名 概略
add_predictions Add predictions to a data frame
add_predictors Add predictors to a formula
add_residuals Add residuals to a data frame
bootstrap Generate 'n' bootstrap replicates.
crossv_mc Generate cross-validated test-training pairs
data_grid Generate a data grid.
fit_with Fit a list of formulas
formulas Create a list of formulas
geom_ref_line Add a reference line (ggplot2).
heights Height and income data.
model-quality Compute model quality for a given dataset
model_matrix Construct a design matrix
na.warn Handle missing values with a warning
resample A "lazy" resample.
resample_bootstrap Generate a boostrap replicate
resample_partition Generate an exclusive partitioning of a data frame
seq_range Generate a sequence over the range of a vector
sim Simple simulated datasets
typical Find the typical value

add_predictions

> df <- tibble::data_frame(
+   x = sort(runif(100)),
+   y = 5 * x + 0.5 * x ^ 2 + 3 + rnorm(length(x))
+ )
> # plot(df)
> 
> m1 <- lm(y ~ x, data = df)
> grid <- data.frame(x = seq(0, 1, length = 10))
> grid %>% add_predictions(m1)
                x          pred
1  0.000000000000 2.71363192798
2  0.111111111111 3.37554990755
3  0.222222222222 4.03746788712
4  0.333333333333 4.69938586668
5  0.444444444444 5.36130384625
6  0.555555555556 6.02322182582
7  0.666666666667 6.68513980539
8  0.777777777778 7.34705778495
9  0.888888888889 8.00897576452
10 1.000000000000 8.67089374409

add_predictors

> f <- lhs ~ rhs
> add_predictors(f, ~var1, ~var2)
lhs ~ rhs + var1 + var2
<environment: 0x11bc237b8>

bootstrap

> library(purrr)
> boot <- bootstrap(mtcars, 100)

typical

> set.seed(71)
> x <- rpois(100, lambda = 10)
> typical(x)
[1] 9.5
> median(x)
[1] 9.5
> x <- sample(c("a", "b", "c"), 100, prob = c(0.6, 0.2, 0.2), replace = TRUE)
> typical(x)
[1] "a"
> table(x)
x
 a  b  c 
64 19 17