Skip to content

Instantly share code, notes, and snippets.

View benwhalley's full-sized avatar

Ben Whalley benwhalley

  • Plymouth University
View GitHub Profile
extract_sims <- function(modelInterval){
# Grabs simulation draws from a predictInterval object.
attr(modelInterval, "sim.results")
}
bind_sims <- function(sims, newdata){
# Where you have simulated predictions based the model or new dataset, it can
# be helpful to combine these with the dataset.
# There may also be multiple draws from the simulation, stored in separate
# columns. These are melted into long form.
. replace adself_2_11=1 if patient==20617 & month==0
(0 real changes made)
. replace adself_2_11=1 if patient==20271 & month==0
(0 real changes made)
. replace adself_2_11=1 if patient==20509 & month==0
(0 real changes made)
('A+', 100)
('A', 88)
('A-', 77)
('B+', 68)
('B', 65)
('B-', 62)
('C+', 58)
('C', 55)
('C-', 52)
('D+', 48)
msc_ids <- c(10308927, 10536302, 10461083, 10114629, 10534788, 10540000, 10539361, 10409295, 206408, 10538988, 755020, 10118522, 390769)
mscmarks <- read.csv("Marks_Yinghui2.csv") %>% filter(Reg.. %in% msc_ids)
mscmarks
NO ROWS..
# https://en.wikipedia.org/wiki/Akaike_information_criterion#How_to_apply_AIC_in_practice
compareAIC <- function(..., models) {
mods = c(list(...), models)
aics <- sapply(mods, FUN = AIC)
round(exp((min(aics) - aics) / 2), 2)
}
# http://stackoverflow.com/questions/11693599/alternative-to-expand-grid-for-data-frames
expand.grid.df <-
function(...)
Reduce(function(...)
merge(..., by = NULL), list(...))
@benwhalley
benwhalley / icc.R
Created June 3, 2016 07:20
Function to calculate ICC for lmer model object
# calculate ICC for lmer model object
icc <- function(m){
vc <- as.data.frame((VarCorr(m)))
l <- vc$vcov
data_frame(grp=vc$grp, icc=sapply(l, function(x){x/sum(l)}))
}
@benwhalley
benwhalley / dcastMult.R
Created April 4, 2016 10:48
dcastMult.R
# http://stackoverflow.com/questions/21477040/reshape2-multiple-results-of-aggregation-function
dcastMult <- function(data, formula, value.var = "value",
funs = list("min" = min, "max" = max)) {
require(reshape2)
if (is.null(names(funs)) | any(names(funs) == "")) stop("funs must be named")
Form <- formula(formula)
LHS <- as.character(Form[[2]])
if (length(LHS) > 1) LHS <- LHS[-1]
temp <- lapply(seq_along(funs), function(Z) {
# http://stackoverflow.com/questions/21477040/reshape2-multiple-results-of-aggregation-function
dcastMult <- function(data, formula, value.var = "value",
funs = list("min" = min, "max" = max)) {
require(reshape2)
if (is.null(names(funs)) | any(names(funs) == "")) stop("funs must be named")
Form <- formula(formula)
LHS <- as.character(Form[[2]])
if (length(LHS) > 1) LHS <- LHS[-1]
temp <- lapply(seq_along(funs), function(Z) {
# effect on total variance
library(dplyr)
N=10000
prob <- .25
e_var = 1
u_var = .25*e_var # see http://www.wolframalpha.com/input/?i=.1%3Db%2F%28a%2Bb%29
icc = u_var/(u_var+e_var)