Closed njtierney closed 1 year ago
The interface would look something like this:
polymod_contact_data <- get_polymod_setting_data()
polymod_survey_data <- get_polymod_population()
setting_models <- fit_setting_contacts(
contact_data_list = polymod_contact_data,
population = polymod_survey_data
)
fairfield_age_pop <- abs_age_lga("Fairfield (C)")
synthetic_settings_5y_fairfield <- predict_setting_contacts(
population = fairfield_age_pop,
contact_model = setting_models,
age_breaks = c(seq(0, 85, by = 5), Inf)
)
# instead of this
plot_matrix(synthetic_settings_5y_fairfield$home)
# you write
autoplot(synthetic_settings_5y_fairfield$home)
# And under the hood, the class of `synthetic_settings_5y_fairfield$home`
# is something like `predicted_setting_contacts`
A bit more detail on implementing autoplot
fit <- lm(mpg ~ cyl, data = mtcars)
summary(mtcars)
#> mpg cyl disp hp
#> Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
#> 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
#> Median :19.20 Median :6.000 Median :196.3 Median :123.0
#> Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
#> 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
#> Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
#> drat wt qsec vs
#> Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
#> 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
#> Median :3.695 Median :3.325 Median :17.71 Median :0.0000
#> Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
#> 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
#> Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
#> am gear carb
#> Min. :0.0000 Min. :3.000 Min. :1.000
#> 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
#> Median :0.0000 Median :4.000 Median :2.000
#> Mean :0.4062 Mean :3.688 Mean :2.812
#> 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
#> Max. :1.0000 Max. :5.000 Max. :8.000
summary(fit)
#>
#> Call:
#> lm(formula = mpg ~ cyl, data = mtcars)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -4.9814 -2.1185 0.2217 1.0717 7.5186
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 37.8846 2.0738 18.27 < 2e-16 ***
#> cyl -2.8758 0.3224 -8.92 6.11e-10 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 3.206 on 30 degrees of freedom
#> Multiple R-squared: 0.7262, Adjusted R-squared: 0.7171
#> F-statistic: 79.56 on 1 and 30 DF, p-value: 6.113e-10
summary.data.frame(mtcars)
#> mpg cyl disp hp
#> Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
#> 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
#> Median :19.20 Median :6.000 Median :196.3 Median :123.0
#> Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
#> 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
#> Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
#> drat wt qsec vs
#> Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
#> 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
#> Median :3.695 Median :3.325 Median :17.71 Median :0.0000
#> Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
#> 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
#> Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
#> am gear carb
#> Min. :0.0000 Min. :3.000 Min. :1.000
#> 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
#> Median :0.0000 Median :4.000 Median :2.000
#> Mean :0.4062 Mean :3.688 Mean :2.812
#> 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
#> Max. :1.0000 Max. :5.000 Max. :8.000
summary.lm(fit)
#>
#> Call:
#> lm(formula = mpg ~ cyl, data = mtcars)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -4.9814 -2.1185 0.2217 1.0717 7.5186
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 37.8846 2.0738 18.27 < 2e-16 ***
#> cyl -2.8758 0.3224 -8.92 6.11e-10 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 3.206 on 30 degrees of freedom
#> Multiple R-squared: 0.7262, Adjusted R-squared: 0.7171
#> F-statistic: 79.56 on 1 and 30 DF, p-value: 6.113e-10
summary.my_class <- function(x, ...){
# my own code here
paste0("This is your object, named ", class(x))
}
# so this part would happen just before the "return" part of the function
# to assign the class
# importantly, you want to retain the original class, but put your new class
# first
class(fit) <- c("my_class", class(fit))
class(fit)
#> [1] "my_class" "lm"
summary(fit)
#> [1] "This is your object, named my_class" "This is your object, named lm"
plot(fit)
library(ggplot2)
autoplot(fit)
#> Error in `autoplot()`:
#> ! Objects of type my_class/lm not supported by autoplot.
autoplot.my_class <- function(object, ...){
plot_df <- tibble::tibble(
predictions = predict(object),
residuals = residuals(object),
) |>
tibble::rownames_to_column()
ggplot(plot_df,
aes(x = predictions,
y = residuals)) +
geom_point()
}
autoplot(fit)
Created on 2022-09-06 by the reprex package (v2.0.1)
to avoid
plot_setting_matrix
perhaps?