forked from rahul-raoniar/The_Researchers_Guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBroom.Rmd
More file actions
185 lines (116 loc) · 2.67 KB
/
Broom.Rmd
File metadata and controls
185 lines (116 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
title: "Broom_package"
author: "Rahul Learning"
date: "27 January 2019"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Broom major functions
1. ###*tidy*: Summarize information about fit components
2. ###*glance*: report goodness of fit measures
3. ###*augment*: add information about observations to a dataset
```{r, message = FALSE}
library(broom)
library(tidyverse)
library(knitr)
```
```{r}
# take a glimpse of the data
glimpse(mtcars)
model1 <- lm(mpg~wt, data = mtcars)
summary(model1)
```
```{r}
# take a glance of the model fitting
glance(model1)
```
```{r}
# take a glance of the model fitted values and stats
augment(model1) %>%
head()
```
```{r, echo = FALSE}
# Print the coefficients
kable2 <- function(data){
knitr::kable(mutate_if(data, is_numeric, round, 2))
}
tidy(model1) %>%
kable2()
```
## Comparing multiple models at once
```{r}
fits <- list(
fit1 <- lm(hp ~ cyl, data = mtcars),
fit2 <- lm(hp ~ cyl + mpg, data = mtcars),
fit3 <- lm(hp ~ ., data = mtcars)
)
gof <- purrr::map_df(fits, glance, .id = "model") %>%
arrange(AIC)
gof
```
## Inspecting residuals from multiple linear regression
```{r}
fit <- lm(hp~., data = mtcars)
au <- broom::augment(fit)
au %>%
head()
p <- au %>%
gather(x, val, -contains(".")) %>%
ggplot(aes(x = val, y = .fitted)) +
geom_point() +
facet_wrap(~x, scales = "free") +
labs(x = "Predictpr values", y = "Residuals") +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank())
p
```
## Bootstrapping examples
```{r}
library(rsample)
boots <- bootstraps(mtcars, times = 100)
boots
fit_nls_on_bootstrap <- function(split){
nls( # non linear least square
mpg ~ k / wt + b,
analysis(split), # Convert a rsplit object to dataframe
start = list(k = 1, b = 0)
)
}
# Fitting model on bootstrap datasets
boot_fits <- boots %>%
mutate(fit = map(splits, fit_nls_on_bootstrap),
coef_info = map(fit, tidy))
boot_fits
# Unnesting coefficient info
boot_coefs <- boot_fits %>%
unnest(coef_info)
boot_coefs
```
## Plotting uncertainity
```{r}
p <- ggplot(boot_coefs, aes(x = estimate)) +
geom_histogram(binwidth = 2) +
facet_wrap(~ term, scales = "free") +
labs(
title = "Sampling distribution of k and b",
y = "count",
x = "Value"
)
p
```
## Visualizing uncertainity and predictions
```{r}
boot_aug <- boot_fits %>%
mutate(augmented = map(fit, augment)) %>%
unnest(augmented)
boot_aug %>%
head()
p <- ggplot(boot_aug, aes(x = wt, y = mpg)) +
geom_point() +
geom_line(aes(y = .fitted, group = id), alpha = 0.2)
p
```