Skip to content

Instantly share code, notes, and snippets.

View vasishth's full-sized avatar
💭
My status is high.

Shravan Vasishth vasishth

💭
My status is high.
View GitHub Profile
@vasishth
vasishth / gist:8065594
Created December 21, 2013 04:50
Stan model of Kliegl et al 2011, version 3
data {
int<lower=1> N;
real rt[N]; //outcome
real c1[N]; //predictor
real c2[N]; //predictor
real c3[N]; //predictor
int<lower=1> I; //number of subjects
int<lower=1, upper=I> id[N]; //subject id
vector[4] mu_prior; //vector of zeros passed in from R
}
@vasishth
vasishth / gist:8012211
Created December 17, 2013 20:39
lmer vs Stan comparison
Details:
key:
beta[1] = Intercept
beta[2] = c1
beta[3] = c2
beta[4] = c3
1. Fixed effects, Stan vs lmer:
Stan:
@vasishth
vasishth / gist:7879095
Created December 9, 2013 19:18
Hierarchical linear model (varying intercepts model) with t distribution instead of normal
model
{
# The model for each observational unit
# (each row is a subject's data point)
for( j in 1:N )
{
mu[j] <- beta[1] + beta[2] * ( so[j] ) + u[subj[j]] + w[item[j]]
rt[j] ~ dnorm( mu[j], tau.e )
##change the above line to:
#rt[j] ~ dt(mu[j],tau.e, 2)
@vasishth
vasishth / gist:7879003
Created December 9, 2013 19:13
Speed of light data revisited.
newcomb <-
c(28,26,33,24,34,-44,27,16,40,-2,
29,22,24,21,25,30,23,29,31,19,
24,20,36,32,36,28,25,21,28,29,
37,25,28,26,30,32,36,26,30,22,
36,23,27,27,28,27,31,27,26,33,
26,32,32,24,39,28,24,25,32,25,
29,27,28,29,16,23)
# Data as a list
@vasishth
vasishth / blups.R
Created March 15, 2013 20:46
BLUPs
> # Calculate the predicted random effects by hand for the ergoStool data
> (fm1<-lmer(effort~Type-1 + (1|Subject),ergoStool))
Linear mixed model fit by REML
Formula: effort ~ Type - 1 + (1 | Subject)
Data: ergoStool
AIC BIC logLik deviance REMLdev
133 143 -60.6 122 121
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 1.78 1.33
@vasishth
vasishth / corr_fixedef_lm.R
Last active December 15, 2015 00:28
linear model correlations of fixed effects
> summary(lm<-lm(wear~material-1,BHHshoes))
> X<-model.matrix(lm)
> 2.49^2*solve(t(X)%*%X)
materialA materialB
materialA 0.62001 0.00000
materialB 0.00000 0.62001
@vasishth
vasishth / corr_fixedeff_example2.R
Created March 15, 2013 19:50
Calculating correlations of fixed effects in LMEs
> b1.vals<-subset(BHHshoes,material=="A")$wear
> b2.vals<-subset(BHHshoes,material=="B")$wear
>
> vcovmatrix<-var(cbind(b1.vals,b2.vals))
>
> covar<-vcovmatrix[1,2]
> sds<-sqrt(diag(vcovmatrix))
> covar/(sds[1]*sds[2])
b1.vals
0.98823
@vasishth
vasishth / corr_fixedeff_example1.R
Created March 15, 2013 19:48
Example of correlations of fixed effects
> (lm.full<-lmer(wear~material-1+(1|Subject), data = BHHshoes))
Linear mixed model fit by REML
Formula: wear ~ material - 1 + (1 | Subject)
Data: BHHshoes
AIC BIC logLik deviance REMLdev
62.9 66.9 -27.5 53.8 54.9
Random effects:
Groups Name Variance Std.Dev.
Subject (Intercept) 6.1009 2.470
Residual 0.0749 0.274