Comments (7)
from example-models.
Here is the model. When using RSTAN, I can get a linear model with factors to run properly. What I cant get, however, is estimates for all means through using the coefficients to generate quantities. Additionally, this model generates standard deviations for each group. I think the problem is in k-1 coding systems for categorical variables.
modelstring = "
model {
for ( i in 1:Ntotal ) {
y[i] ~ dt( mu[i] , 1/(ySigma[x1[i],x2[i]])^2 , nu )
mu[i] <- a0 + a1[x1[i]] + a2[x2[i]] + a1a2[x1[i],x2[i]]
}
# nu ~ dgamma(5.83,0.0483) # mode 100, sd 50
nu ~ dexp(1/30.0)
#
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
sigma[j1,j2] ~ dgamma( sigmaSh , sigmaRa )
# Prevent from dropping too close to zero:
ySigma[j1,j2] <- max( sigma[j1,j2] , medianCellSD/1000 )
} }
sigmaSh <- 1 + sigmaMode * sigmaRa
sigmaRa <- ( sigmaMode + sqrt( sigmaMode^2 + 4_sigmaSD^2 ) ) /(2_sigmaSD^2)
sigmaMode ~ dgamma(sGammaShRa[1],sGammaShRa[2])
sigmaSD ~ dgamma(sGammaShRa[1],sGammaShRa[2])
#
a0 ~ dnorm( yMean , 1/(ySD*5)^2 )
#
for ( j1 in 1:Nx1Lvl ) { a1[j1] ~ dnorm( 0.0 , 1/a1SD^2 ) }
a1SD ~ dgamma(aGammaShRa[1],aGammaShRa[2])
#
for ( j2 in 1:Nx2Lvl ) { a2[j2] ~ dnorm( 0.0 , 1/a2SD^2 ) }
a2SD ~ dgamma(aGammaShRa[1],aGammaShRa[2])
#
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
a1a2[j1,j2] ~ dnorm( 0.0 , 1/a1a2SD^2 )
} }
a1a2SD ~ dgamma(aGammaShRa[1],aGammaShRa[2]) # or try a folded t (Cauchy)
# Convert a0,a1[],a2[],a1a2[,] to sum-to-zero b0,b1[],b2[],b1b2[,] :
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
m[j1,j2] <- a0 + a1[j1] + a2[j2] + a1a2[j1,j2] # cell means
} }
b0 <- mean( m[1:Nx1Lvl,1:Nx2Lvl] )
for ( j1 in 1:Nx1Lvl ) { b1[j1] <- mean( m[j1,1:Nx2Lvl] ) - b0 }
for ( j2 in 1:Nx2Lvl ) { b2[j2] <- mean( m[1:Nx1Lvl,j2] ) - b0 }
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
b1b2[j1,j2] <- m[j1,j2] - ( b0 + b1[j1] + b2[j2] )
} }
} (Kruschke, 2015)
from example-models.
Hi, you should include the Stan model, not the Bugs model!
A
On Apr 9, 2016, at 6:19 PM, wdonald1985 [email protected] wrote:
Here is the model. When using RSTAN, I can get a linear model with factors to run properly. What I cant get, however, is estimates for all means through using the coefficients to generate quantities. Additionally, this model generates standard deviations for each group. I think the problem is in k-1 coding systems for categorical variables.
modelstring = "
model {
for ( i in 1:Ntotal ) {
y[i] ~ dt( mu[i] , 1/(ySigma[x1[i],x2[i]])^2 , nu )
mu[i] <- a0 + a1[x1[i]] + a2[x2[i]] + a1a2[x1[i],x2[i]]
}nu ~ dgamma(5.83,0.0483) # mode 100, sd 50
nu ~ dexp(1/30.0)
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
sigma[j1,j2] ~ dgamma( sigmaSh , sigmaRa )Prevent from dropping too close to zero:
ySigma[j1,j2] <- max( sigma[j1,j2] , medianCellSD/1000 )
} }
sigmaSh <- 1 + sigmaMode * sigmaRa
sigmaRa <- ( sigmaMode + sqrt( sigmaMode^2 + 4sigmaSD^2 ) ) /(2sigmaSD^2)
sigmaMode ~ dgamma(sGammaShRa[1],sGammaShRa[2])
sigmaSD ~ dgamma(sGammaShRa[1],sGammaShRa[2])a0 ~ dnorm( yMean , 1/(ySD*5)^2 )
for ( j1 in 1:Nx1Lvl ) { a1[j1] ~ dnorm( 0.0 , 1/a1SD^2 ) }
a1SD ~ dgamma(aGammaShRa[1],aGammaShRa[2])for ( j2 in 1:Nx2Lvl ) { a2[j2] ~ dnorm( 0.0 , 1/a2SD^2 ) }
a2SD ~ dgamma(aGammaShRa[1],aGammaShRa[2])for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
a1a2[j1,j2] ~ dnorm( 0.0 , 1/a1a2SD^2 )
} }
a1a2SD ~ dgamma(aGammaShRa[1],aGammaShRa[2]) # or try a folded t (Cauchy)Convert a0,a1[],a2[],a1a2[,] to sum-to-zero b0,b1[],b2[],b1b2[,] :
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
m[j1,j2] <- a0 + a1[j1] + a2[j2] + a1a2[j1,j2] # cell means
} }
b0 <- mean( m[1:Nx1Lvl,1:Nx2Lvl] )
for ( j1 in 1:Nx1Lvl ) { b1[j1] <- mean( m[j1,1:Nx2Lvl] ) - b0 }
for ( j2 in 1:Nx2Lvl ) { b2[j2] <- mean( m[1:Nx1Lvl,j2] ) - b0 }
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
b1b2[j1,j2] <- m[j1,j2] - ( b0 + b1[j1] + b2[j2] )} }
} (Kruschke, 2015)—
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub #61 (comment)
from example-models.
Is that the right attached program? I don't see
any categorical variables.
Consistent indentation on code makes it much easier
to read. I may just be missing something.
- Bob
On Apr 9, 2016, at 6:19 PM, wdonald1985 [email protected] wrote:
Here is the model. When using RSTAN, I can get a linear model with factors to run properly. What I cant get, however, is estimates for all means through using the coefficients to generate quantities. Additionally, this model generates standard deviations for each group. I think the problem is in k-1 coding systems for categorical variables.
modelstring = "
model {
for ( i in 1:Ntotal ) {
y[i] ~ dt( mu[i] , 1/(ySigma[x1[i],x2[i]])^2 , nu )
mu[i] <- a0 + a1[x1[i]] + a2[x2[i]] + a1a2[x1[i],x2[i]]
}nu ~ dgamma(5.83,0.0483) # mode 100, sd 50
nu ~ dexp(1/30.0)
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
sigma[j1,j2] ~ dgamma( sigmaSh , sigmaRa )Prevent from dropping too close to zero:
ySigma[j1,j2] <- max( sigma[j1,j2] , medianCellSD/1000 )
} }
sigmaSh <- 1 + sigmaMode * sigmaRa
sigmaRa <- ( sigmaMode + sqrt( sigmaMode^2 + 4sigmaSD^2 ) ) /(2sigmaSD^2)
sigmaMode ~ dgamma(sGammaShRa[1],sGammaShRa[2])
sigmaSD ~ dgamma(sGammaShRa[1],sGammaShRa[2])a0 ~ dnorm( yMean , 1/(ySD*5)^2 )
for ( j1 in 1:Nx1Lvl ) { a1[j1] ~ dnorm( 0.0 , 1/a1SD^2 ) }
a1SD ~ dgamma(aGammaShRa[1],aGammaShRa[2])for ( j2 in 1:Nx2Lvl ) { a2[j2] ~ dnorm( 0.0 , 1/a2SD^2 ) }
a2SD ~ dgamma(aGammaShRa[1],aGammaShRa[2])for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
a1a2[j1,j2] ~ dnorm( 0.0 , 1/a1a2SD^2 )
} }
a1a2SD ~ dgamma(aGammaShRa[1],aGammaShRa[2]) # or try a folded t (Cauchy)Convert a0,a1[],a2[],a1a2[,] to sum-to-zero b0,b1[],b2[],b1b2[,] :
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
m[j1,j2] <- a0 + a1[j1] + a2[j2] + a1a2[j1,j2] # cell means
} }
b0 <- mean( m[1:Nx1Lvl,1:Nx2Lvl] )
for ( j1 in 1:Nx1Lvl ) { b1[j1] <- mean( m[j1,1:Nx2Lvl] ) - b0 }
for ( j2 in 1:Nx2Lvl ) { b2[j2] <- mean( m[1:Nx1Lvl,j2] ) - b0 }
for ( j1 in 1:Nx1Lvl ) { for ( j2 in 1:Nx2Lvl ) {
b1b2[j1,j2] <- m[j1,j2] - ( b0 + b1[j1] + b2[j2] )} }
} (Kruschke, 2015)—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub
from example-models.
Through trial and many errors my RSTAN model is not looking very good. I'll post it after I clean it up a bit. Thanks!
from example-models.
Was there a JAGS model you're trying to translate? The one you sent
didn't have any categorical variables I could find.
- Bob
P.S. I'd strongly recommend version control to recover clean old versions
of things. Where possible, I'd also recommend starting with the simplest
thing that works and building out rather than trying to build a complicated
model all at once.
On Apr 10, 2016, at 1:38 PM, wdonald1985 [email protected] wrote:
Through trial and many errors my RSTAN model is not looking very good. I'll post it after I clean it up a bit. Thanks!
—
You are receiving this because you commented.
Reply to this email directly or view it on GitHub
from example-models.
Hello:
Apologies on taking so long to provide the script to my model. Since you are both very well know statisticians, I wanted to ensure my code was up to par. This is a 3 * 2 interaction with categorical predictors. Since I am only getting one estimate for sigma, I think my model is assuming homogeneous variances. I want a model that does not make this assumption, thereby providing 6 estimates for sigma. Additionally, I would like a way to get the lower level estimates, for example, estimates for VS and GEAR without having to build another model.
myData <- mtcars
X <- model.matrix(~ vs * gear, data = myData)
K <- 6 // number of columns in matrix
N <- length(mtcars$mpg)
standat= list(X = X,K =K, N =N, y =y)
stanmodelcode = '
data {
int<lower=0> N;
vector[N] y;
int<lower=0> K;
matrix [N, K]X ;
}
parameters {
real <lower =0> sigma;
vector [K] beta;
}
transformed parameters {
}
model {
y ~ normal(X * beta, sigma);
}
generated quantities {
real mu0_3;
real mu1_3;
real mu0_4;
real mu1_4;
real mu0_5;
real mu1_5;
mu0_3 <- beta[1];
mu1_3 <- beta[1] + beta[2];
mu0_4 <- beta[1] + beta[3];
mu1_4 <- beta[1] + beta[2] + beta[3] + beta[5];
mu0_5 <- beta[1] + beta [4];
mu1_5 <- beta[1] + beta[2] + beta[4] + beta[6];
}
'
fit = stan(model_code=stanmodelcode, data=standat, iter=1200, warmup=10, seed = 1,
chains=2, thin=2)
from example-models.
Related Issues (20)
- `stan_demo` example 500 not working HOT 4
- Missing function in Introduction
- Error in BPA Ch.10 js_super.stan with possible fix HOT 1
- rats ref link at top of stan files is broken
- Add .data.R files for knitr models HOT 1
- Rdump format
- Reorganize the stan example repository HOT 13
- sampling() fails in Lotka-Volterra example HOT 3
- Lotka-Volterra example not running HOT 1
- BUGS Background Information for Litter Example HOT 1
- Failed to build Lotka Vollterra on Mac OS / clang HOT 9
- reduce_sum case study will be out of date once new cmdstanr pull gets merged
- Example script `fit_nyc_bym2.R` to use model `bym2_offset_only.stan` doesn't work HOT 1
- Subtle error in some capture-recapture likelihoods HOT 3
- [Error] Validate transformed params
- Survey.stan example from Cognitive Modeling - Expected end of file after end of generated quantities block. HOT 9
- Which book/chapter HOT 1
- updates to ARM model for normal_id_glm - variable name "cov" should be "x_as_mat" or something like that HOT 6
- add Python scripts to BYM12 case study HOT 5
- Parameter names for stan file HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from example-models.