I am trying to run a trial that runs a couple of changing variables through a poisson distribution and gives an output. It goes through three years and there are 12 clusters. All of the groups 1-6 are control and 7 to 12 are intervention.
Year one would have clusters 1-12. with 6 control and 6 intervention. same for year two and three. So in year one you get a v and that v is the same for each cluster but each cluster has a different lambda.
This is the basic code and equation.
mu=3.4
lambda=rnorm(1,0,.27)
v=rnorm(1,0,.53)
e=rnorm(1,0,.74)
x=rpois(1, exp(mu+lambda+v+e))
Lambda and v are normally distributed with those st. deviations for each group. I would like in say year 1 for it to take v=rnorm(1,0,.53) and run that through the equation. At the same time I want it to take lambda=rnorm(1,0,.27) and run that for each of the 12 clusters with the same v from year one. So basically one v and 12 lambdas per year. I believe this can be done with functions but I am having a difficult time with it. I tried using for loops but I don't think it is doing exactly what I want. Here is the code with the for loops:
mu=3.4
lambda=numeric(length=12)
v=numeric(length=3)
for (i in 1:120{
lambda[i]=rnorm(1,0,.27)
}
for (j in 1:3){
v[j]=rnorm(1,0,.53)
}
e=rnorm(1,0,.74)
x=rpois(1, exp(mu+lambda+v+e))
I am not really sure if this is doing that correct thing or not so if someone with more experience could explain what my code is doing and how to make it do what I want with functions for lambda and v I would greatly appreciate it. Thanks for the help.
It sounds like you have 12 groups and 3 years for a total of 36 data points. For each year i you have a single value v[i] and 12 group-specific values lambda[1], lambda[2], ..., lambda[12]. You didn't really specify how each e is drawn, so let's assume there's a global e value.
You can build up a data frame with the relevant parameters for every group and year:
Then you can specify each parameter in your data frame:
Finally you can compute the x values from the poisson distribution based on all the other specified parameters.