Skip to content

Instantly share code, notes, and snippets.

View JWiley's full-sized avatar

Joshua F. Wiley, Ph.D. JWiley

View GitHub Profile
@JWiley
JWiley / fittedvalues.R
Created October 26, 2021 09:00
R code to simulate and fit a random intercept linear model showing the fixed intercept and average of fitted values credible intervals do not align.
library(brms)
library(bayestestR)
#### Simulate some data ####
nID <- 100 ## number of people
k <- 4 ## number of observations per person
## make it reproducible
set.seed(1234)
@JWiley
JWiley / mp-bc
Created September 6, 2016 13:17
USEVARIABLES =
FAM_SUPT ISEL_ALL
ECEA6 PIEA17 SEEA8
sash_langnew SASH_SOC
screen
! covariates
HCE2 MHEA26
AGE US_BORN
CENTER2
CENTER3
@JWiley
JWiley / parallel_boot_test.R
Created April 14, 2015 21:33
Parallel Rcpp Bootstrap lm function test
set.seed(1234)
dmat <- cbind(1, matrix(rnorm(1000 * 50), ncol = 50))
beta <- matrix(c(2, runif(50)))
yvec <- as.vector(dmat %*% beta + rnorm(1000, sd = 3))
dall <- cbind(y = yvec, as.data.frame(dmat[,-1]))
myindex <- matrix(sample(0:(1000 - 1), 1000 * 500, TRUE), ncol = 500)
library(Rcpp)
library(RcppEigen)
library(RcppParallel)
@JWiley
JWiley / parallel_boot_lm.cpp
Last active August 4, 2019 19:35
Rcpp Eigen and Parallel based parallel linear model bootstrap
// [[Rcpp::depends(RcppParallel)]]
// [[Rcpp::depends(RcppEigen)]]
#include <Rcpp.h>
#include <RcppEigen.h>
#include <RcppParallel.h>
using namespace std;
using namespace Rcpp;
using namespace RcppParallel;
@JWiley
JWiley / graph.rmd
Created September 22, 2013 00:00
Clustering and interactive graph
Interactive Graph Example
=========================
Function to do the work. Aside from the packages, to run this, just load the knitr package, and then knit2html("graph.rmd").
```{r}
longplot <- function(dat, k, xlab, ylab, seed=4324, miter = 20) {
require(EMCluster)
require(mice)
require(reshape2)
# import generics
# (cheap version of importFrom() in a package)
fixef <- nlme:::fixef
ranef <- nlme:::ranef
# define generic prediction function
# but using my functions/methods
predict2 <- function (object, ...) {
UseMethod("predict2")
}
@JWiley
JWiley / scoping.R
Created July 7, 2012 10:14
Scoping Fun in R
## In the end, what is the twelth column of 'dat', a, and b?
## why does the local() not matter in thirteen?
a <- 10
b <- 10
dat <- local(
data.frame(
first = b,
second = a <- 0 -> b,
third = a <- rnorm(10, mean = a),
fourth = a / 2,
@JWiley
JWiley / binarySimulator.R
Created February 17, 2012 03:11
function to simulate some binary data
bsim <- function(reps = 10^2, groups = 200, L1k = 6, L2k = 3, seed = 10) {
stopifnot(all.equal(L1k %% L2k, 0L))
f <- function(x) {
mdiff <- min(x, na.rm = TRUE) - 0
(x - mdiff)/(max(x, na.rm = TRUE) - mdiff)
}
N <- reps * groups
dfm <- data.frame(y = c(30, 43, 36, 33, 50, 45, 40) +
scale(rnorm(7, 2, 5), scale = FALSE), x = 1:7)
dalt <- data.frame(x = seq(1, 7, length.out = 100))
m <- lapply(1:6, function(i) {lm(y ~ poly(x = x, degree = i), data = dfm)})
pdf(file = "tmp.pdf", height = 11, width = 8.5, onefile = TRUE)
par(mfrow = c(3, 2))
for(i in as.numeric(1:6)) {
plot(y ~ x, data = dfm, main =
navel.gazer <- function(month = NULL, year = NULL, entire = FALSE,
list = "r-help", n = 50, plot = TRUE) {
# Ben Bolker came up with most of the code
# Henrique Dallazuanna provided an edit to the z <- line of code
# Brian Diggs provided capwords() to properly count Peter Dalgaard
# Joshua Wiley adapted all of it to one function
if(is.null(month)) {
month <- format(Sys.Date(), format = "%B")