]]>

If you save the top figure from here to a PNG called “Fig2a_Kreners2015” in your working directory, you can digitize the three lines and replicate the figure (or do other stuff if you so wish) as follows

library(digitize) dig10 <- digitize("Fig2a_Kremers2015.png") # do the solid line dig15 <- digitize("Fig2a_Kremers2015.png") # do the dashed line dig20 <- digitize("Fig2a_Kremers2015.png") # do the dotted line # rename the first variable names(dig10)[1] <- names(dig15)[1] <- names(dig20)[1] <- "age" plot(dig10, type = "l") lines(dig15, lty = 2) lines(dig20, lty = 3)

Easy huh? It just gets a bit laborious with many points…

]]>

Note that we’re comparing the precision (tau) rather than the variance or SD… SD = 1/sqrt(tau)

This file contains 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

# | |

# Compare lmer and inla for LMM | |

# largely taken from Spatial and spatio-temporal bayesian models with R-INLA (Blangiardo & Cameletti, 2015), section 5.4.2 | |

# | |

m <- 10000 # N obs | |

set.seed(1234) | |

x <- rnorm(m) | |

group <- sample(seq(1, 100), size = m, replace = TRUE) | |

# simulate random intercept | |

tau.ri <- .25 | |

1/sqrt(tau.ri) #SD | |

set.seed(4567) | |

v <- rnorm(length(unique(group)), 0, sqrt(1/tau.ri)) | |

# assign random intercept to individuals | |

vj <- v[group] | |

# simulate y | |

tau <- 3 | |

1/sqrt(tau) #SD | |

set.seed(8910) | |

b0 <- 5 | |

beta1 <- 2 | |

y <- rnorm(m, b0 + beta1*x + vj, 1/sqrt(tau)) | |

library(lme4) | |

mod <- lmer(y ~ x + (1|group)) | |

summary(mod) | |

vc <- VarCorr(mod) | |

library(INLA) | |

form <- y ~ x + f(group, model = "iid", param = c(1, 5e-5)) | |

imod <- inla(form, family = "gaussian", | |

data = data.frame(y = y, x = x, group = group)) | |

summary(imod) | |

cbind(truth = c(tau, tau.ri), lmer = 1/c(attr(vc, "sc")^2, unlist(vc)), inla = imod$summary.hyperpar$`0.5quant`) | |

plot(imod, | |

plot.fixed.effects = F, | |

plot.lincomb = F, | |

plot.random.effects = F, | |

plot.predictor = F, | |

plot.prior = TRUE) | |

plot(imod$marginals.hyperpar$`Precision for the Gaussian observations`, type = "l") | |

# the equivalent of this for lmer is not easy to get at all. One would have to profile the deviance function (see http://lme4.r-forge.r-project.org/slides/2009-07-16-Munich/Precision-4.pdf) | |

lo <- imod$summary.hyperpar$`0.025quant`[1] | |

hi <- imod$summary.hyperpar$`0.975quant`[1] | |

dd <- imod$marginals.hyperpar$`Precision for the Gaussian observations` | |

dd <- dd[dd[,1] >= lo & dd[,1] <= hi, ] | |

dd <- rbind(dd, c(hi, 0)) | |

dd <- rbind(dd, c(lo, 0)) | |

polygon(dd, col = "blue") | |

plot(imod$marginals.hyperpar$`Precision for group`, type = "l") | |

lo <- imod$summary.hyperpar$`0.025quant`[2] | |

hi <- imod$summary.hyperpar$`0.975quant`[2] | |

dd <- imod$marginals.hyperpar$`Precision for group` | |

dd <- dd[dd[,1] >= lo & dd[,1] <= hi, ] | |

dd <- rbind(dd, c(hi, 0)) | |

dd <- rbind(dd, c(lo, 0)) | |

polygon(dd, col = "blue") | |

As you’d hope, the results come pretty close to each other and the truth:

cbind(truth = c(tau, tau.ri), lmer = 1/c(attr(vc, "sc")^2, unlist(vc)), inla = imod$summary.hyperpar$`0.5quant`) truth lmer inla 3.00 2.9552444 2.9556383 group 0.25 0.2883351 0.2919622

Code on Github…

]]>It certainly seems to be flexible… putting in the same date in 8 different formats all yielded the same result! (FWIW, “15th October 2010” doesn’t work…)

> anytime::anydate(c("2010-10-15", "2010-Oct-15", + "20101015", "15 Oct 2010", + "10-15-2010", "15 October 2010", + "15oct2010", "2010oct15", "15th October 2010")) [1] "2010-10-15" "2010-10-15" "2010-10-15" "2010-10-15" "2010-10-15" "2010-10-15" [7] "2010-10-15" "2010-10-15" NA

There’s an equivalent function for times (anytime instead of anydate). Looks like working with dates and times just got easier!

]]>

x <- 0.5 # center x y <- 0.5 # center y n <- 1000 # nr of pts r <- 0.5 # radius pts <- seq(0, 2 * pi, length.out = n) plot(sin(pts), cos(pts), type = 'l', asp = 1) # test require(sp) xy <- cbind(x + r * sin(pts), y + r * cos(pts)) sl <- SpatialPolygons(list(Polygons(list(Polygon(xy)), "polygon"))) plot(sl, add=FALSE, col = 'red', axes=T ) # the square xy <- cbind(c(0, 1, 1, 0), c(0, 0, 1, 1)) sq <- SpatialPolygons(list(Polygons(list(Polygon(xy)), "polygon"))) plot(sq, add = TRUE) N <- 1e6 x <- runif(N, 0, 1) y <- runif(N, 0, 1) sp <- SpatialPoints(cbind(x, y)) plot(sp, add = TRUE, col = "green") require(rgeos) (sim_pi <- (sum(gIntersects(sp, sl, byid = TRUE))/N) *4) sim_pi - pi

Note the use of sp and rgeos packages to calculate the intersections.

]]>devtools::install_github("r-lib/crayon") library(crayon)

cat(green( 'I am a green line ' %+% blue$underline$bold('with a blue substring') %+% yellow$italic(' that becomes yellow and italicised!\n') ))]]>

It looks like a super tool!

]]>