Skip to content

Instantly share code, notes, and snippets.

@dill
Last active February 21, 2019 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dill/f5e3aebc9b9246c4bec46f2e61c3ea38 to your computer and use it in GitHub Desktop.
Save dill/f5e3aebc9b9246c4bec46f2e61c3ea38 to your computer and use it in GitHub Desktop.
☁️🎲💻
# generate some relaxing cloudy sky squares
#
# David Lawrence Miller (2019)
library(INLA)
library(ggplot2)
## function to simulate spatial fields
simdat <- function(n, seed, prior.range = c(5, .05),
prior.sigma = c(.05, .05)){
sigma.u <- .1
kappa <- 1
range <- sqrt(8*1.5) / kappa
fake.locations <- as.matrix(expand.grid(seq(0, 10, length.out=n),
seq(0, 10, length.out=n)))
mesh.sim <- inla.mesh.2d(loc = fake.locations, max.n=500)
spde <- inla.spde2.pcmatern(mesh.sim,
prior.range=prior.range,
prior.sigma=prior.sigma)
Qu <- inla.spde.precision(spde, theta=c(log(range), log(sigma.u)))
u <- inla.qsample(n=1, Q=Qu, seed=seed)
u <- u[ ,1]
# project mesh to locations
A <- inla.spde.make.A(mesh=mesh.sim, fake.locations)
u <- drop(A %*% u)
# make a data object
dat <- data.frame(x=fake.locations[,1],
y=fake.locations[,2],
u=u)
return(dat)
}
cloudy_boi <- function(seed=runif(1)*1000){
ggplot(simdat(200, seed)) + geom_tile(aes(x=x,y=y,fill=u)) +
theme_void() + coord_equal() + theme(legend.position="none") +
scale_fill_gradient(high = "#ffffff", low = "#56B1F7")
}
@dill
Copy link
Author

dill commented Feb 21, 2019

screen shot 2019-02-21 at 19 53 58

p114 <- cloudy_boi(114)
p213 <- cloudy_boi(213)
p4144 <- cloudy_boi(4144)
p8912 <- cloudy_boi(8912)

library(gridExtra)
grid.arrange(p114, p213, p4144, p8912, ncol=2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment