simulation in R

Hello, I am trying to do a simulation process, which consists of making a function to generate data, then fit a correlated model and another uncorrelated model with the generated data, this must be repeated a large number of times, for example 100. With the The code shown below only generates one output and I want it to generate the outputs or summaries of all the models adjusted simultaneously, that is, if there are 100 simulations, they should generate 200 summaries, since they are two models. Can you help me, please.

simul=function(var.v = 1.7, var.w = 2.6, corr = 0, n = 217, mean=52.5576, sd= 20.45235, mu=c(0,0), a0 = c(1.9, -0.01), b0 = c(2.5, -0.02)){
cov = sqrt(var.vvar.w)corr
sigma = matrix(c(var.v, cov, cov, var.w), 2, 2)
library(MASS)
u = mvrnorm(n = n, mu, Sigma=sigma)
x = rnorm(n, mean, sd)
X = cbind( rep(1, length(x)), x)
mu1 = exp(X%
%a0 + u[,1])
mu2 = exp(X%
%b0 + u[,2])
y1 = rpois(n, mu1)
y2 = rpois(n, mu2)
y = c(y1, y2)
y
Xb = c(x, x)
id = c(1:n, 1:n)
time = as.factor(c(rep(1, n), rep(2, n)))

plot(y1, y2)

datos=data.frame(y,Xb,time,id)
library(MASS)
library(lme4)
fit1=glm(y ~ Xbtime)
fit2<- glmer(y~Xb
time + (time-1 | id) , family = poisson, data=datos)
summary(fit1)
summary(fit2)
}
simul(var.v = 1.7, var.w = 2.6, corr = 0, n = 217, mean=52.5576, sd= 20.45235, mu=c(0,0), a0 = c(1.9, -0.01), b0 = c(2.5, -0.02))
sims = replicate(100, simul(), simplify = FALSE )
sims

strong text

Hello, I'd like to help you.
I understood from your post that you had a problem with getting only a single output rather than many for the many simulations you wanted, so I thought that I would paste your code in my session, and run it, and then debug it to improve it.
However I'm stuck at the first point because the code in the form you have shared does not run succesfully to give even a single result.
Could you review what you shared and try again ?

Thanks for answering, I already checked the code and removed some lines, now it already generates the simulations that I need but it only generates the summary of the latest model, that is, of fit2, but I also need that of fit1. Could you help me with it? please.

Thanks in advance for your collaboration.

the code remains un-runnable.

Have you yourself tried to run it in a fresh session...?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.