Skip to content

Instantly share code, notes, and snippets.

View benwhalley's full-sized avatar

Ben Whalley benwhalley

  • Plymouth University
View GitHub Profile
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..
('A+', 100)
('A', 88)
('A-', 77)
('B+', 68)
('B', 65)
('B-', 62)
('C+', 58)
('C', 55)
('C-', 52)
('D+', 48)
. 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)
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.
rf2.longs.with <- bind_sims(extract_sims(rf2.preds), newdata)
rf2predswide <- rf2.longs %>%
dcast(., month+sim+pd.b~grp, value.var="fitted", fun.aggregate=mean)
intervals <- rf2predswide %>%
mutate(diff=tau-dbt) %>%
group_by(month) %>%
do(., as.data.frame(t(quantile(.$diff, probs=c(.025, .5, .975)))))
p1 <-
ggplot(group.table, aes(month, fit, group=grp, color=grp, shape=grp)) +
geom_point() + geom_line() +
scale_color_discrete("") + scale_shape_discrete("") +
geom_text(show_guide = FALSE, aes(label=fit, y=fit), size=3) +
xlab(paste("Month")) +
ylab(paste("Model adjusted", toupper(dv))) +
theme(legend.position="top") +
theme(axis.text.x=element_blank(),axis.title.x=element_blank())
@benwhalley
benwhalley / gist:8686bdf8c80550a089f135ee303d7c5f
Last active January 19, 2017 22:23
Using sims from predictInterval to plot intervals around continuous covariate
library(lmerTest)
library(merTools)
library(tidyverse)
# linear model
m <- lmer(Reaction~Days*I(Days^2)+(1|Subject), data=sleepstudy)
newdata = expand.grid(Reaction=NA, Days=1:5, Subject=Inf) %>% as.data.frame
sims <- predictInterval(m, newdata=newdata, returnSims = T)
library(tidyverse)
library(lmerTest)
library(broom)
pwr <- function(N){
simdata <- data_frame(person =1:N, c1=rnorm(N, 20, 25), c2 = rnorm(N, 77, 27)) %>%
reshape2::melt(id.var="person")
lmer(value~variable+(1|person), data=simdata) %>% summary tidy %>%
mutate(N=N)
}
library(tidyverse)
library(lmerTest)
library(broom)
mu_type1 <- 20
mu_type2 <- 70
sd_both <- 20
pwr <- function(N, mu_type1, mu_type2, sd_both){
library(tidyverse)
library(lmerTest)
library(broom)
mu_type1 <- 20
mu_type2 <- 70
sd_both <- 20
pwr <- function(N, mu_type1, mu_type2, sd_both){