This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Apply scale to just numeric variables | |
| #keep all variables | |
| dat2 <- dat %>% | |
| mutate(across(where(is.numeric), scale)) | |
| #keep just numerical variables, drop the others from the data.frame | |
| dat2 <- dat %>% | |
| transmute(across(where(is.numeric), scale)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Demonstration of splines in R | |
| #Can be used to add nonlinearity to linear models (e.g. GLMs) | |
| library(splines) | |
| x <- rnorm(10000) | |
| n_df <- 5 | |
| ns_design_mat <- ns(x, n_df) #n_df degrees of freedom | |
| plot(x, ns_design_mat[,1], ylim = c(-1, 1)) | |
| points(x, ns_design_mat[,2], col = "red") | |
| points(x, ns_design_mat[,3], col = "blue") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Template for doing empirical Bayes prediction from a GAM with mgcv | |
| # CJ Brown 2021-08-23 | |
| # | |
| # See Wood 2017 and ?predict.gam for more info | |
| #First of all its often easiest to create your prediction dataframe with | |
| # visreg, e.g. this makes a df across all values of NEOLI_Total | |
| # and "has_fee": | |
| xfit <- visreg(tour_mod, xvar = "NEOLI_Total", partial = FALSE, | |
| plot = FALSE, by = "has_fee") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Functions to simulate predictions and CIs from a GAM | |
| # CJ Brown | |
| # 2023-03-24 | |
| # | |
| # based on code in ?gam.predict and Wood, S.N. (2017) Generalized Additive Models: An | |
| # Introduction with R (2nd edition). Chapman and | |
| # Hall/CRC. | |
| library(lazyeval) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Simulate from an RW2 (random walk order 2) model. | |
| # Useful for generating predictions from INLA RW2 models. | |
| # When forecast, RW2 models predict continuation of trend, with | |
| # deviations from the trend controlled by the sd parameter. | |
| # | |
| # See: https://inla.r-inla-download.org/r-inla.org/doc/latent/rw2.pdf | |
| # Can intialize the RW2 at a given starting point, this needs | |
| # needs to be two consecutive points, given we are working with | |
| # second order differences. |
OlderNewer