Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

Setting up a model using R2jags

$
0
0

I'm working on a project for an introductory Bayesian analysis course and I'm also fairly new to using R regularly. We are supposed to build a hierarchical model using a data set we found or put together. I put a data set together to analyze the question of how much a nations social freedoms and its wealth affect its happiness. The data set looks like this:

      Country Year Happiness.Score   GDP.PPP Status
1 Afghanistan 2015           3.575  1766.593     NF
2 Afghanistan 2016           3.360  1757.023     NF
3 Afghanistan 2017           3.794  1758.466     NF
4     Albania 2015           4.959 10971.044     PF
5     Albania 2016           4.655 11356.717     PF
6     Albania 2017           4.644 11803.282     PF

Status is taking from Freedom House, NF means not free, PF means partly free, and F means a free society. For my first model, I don't want to add the GDP data.

I put together code that looked the same as an example we saw in class, but I've run into trouble, and I'm not sure how to fix it.

Here is how I set everything up from making lists, to modeling, setting up priors, and running the jags command:

Num.Obs <- length(master.set$Country) #460
Country <- master.set[,1]
Year <- master.set[,2]
Happiness.Score <- master.set[,3]
GDP.PPP <- master.set[,4]
Status <- unique(master.set[,5])
Num.Status <- 3

#Modeling
model1 <- function(){
  #Data Model
  for (i in 1:Num.Obs){
    #Observations at Country Level
    Happiness.Score[i] ~ dnorm(mu[i], tau.Happiness.Score)
    #Random Intercept and slope(alpha is intercetp, beta1 of country and time)
    mu[i] <- alpha[Country[i]] + beta1[Country[i]]*Year[i]
  }
  #Priors
  for (j in 1:Num.Status){
    alpha[j] ~ dnorm(mu.alpha, tau.alpha)
    beta1[j] ~ dnorm(mu.beta1[Status[j]], tau.beta1)
  }
  mu.alpha ~ dnorm(0, 0.01)
  tau.alpha ~ dnorm(0, 0.01)
  sigma.alpha ~ dunif(0, 10)

  mu.beta1[1] ~ dnorm(0, 0.01)
  mu.beta1[2] ~ dnorm(0, 0.01)
  mu.beta1[3] ~ dnorm(0, 0.01)
  tau.beta1 ~ dnorm(0, 0.01)
  sigma.beta1 ~ dunif(0, 10)

  tau.Happiness.Score ~ dnorm(0, 0.01)
  sigma.Happiness.Score ~ dnorm(0,01)
}

#Listing my lists and parameters I want to save for final use
MyVars1data <- list(Country=Country, Year=Year, Status=Status, Happiness.Score=Happiness.Score,
                    Num.Obs=Num.Obs, Num.Status=Num.Status)
params <- c("mu.alpha", "mu.beta1[1]", "mu.beta1[2]", "mu.beta1[3]", "sigma.Happiness.Score")

#Implement Jags
m1 <- jags(MyVars1data,
           model.file = model1,
           parameters.to.save = params,
           n.chains = 3,
           inits = NULL,
           n.iter = 10000,
           n.burnin = 5000,
           progress.bar = "text")

I end up with an error that looks like this:

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Compilation error on line 5.
Index out of range taking subset of  alpha

I have 460 observations in my data set. I'm looking through the loops I made and I think i should be going from 1 to 460. I believe the error is here: mu[i] <- alpha[Country[i]] + beta1[Country[i]]*Year[i] Am I looking at the wrong place?

Can anyone help me to debug this? Any help would be greatly appreciated.


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>