The following is a function of simulating data from simple linear regression and calculating non-coverage probability of each parameters .
and the output is :
I want to compute the mean of each variable ("nc1","nc2") in all replicates, that is mean of nc1 will be mean(c(0,1,0,0)) and mean of nc2 will be mean(c(0,0,0,0)) .
How can I compute the mean by R command ?
Many thanks! Regards.
Code:
simfun <- function(n,b0,b1,sig){
x <- runif(n)
y <- b0+b1*x+ rnorm(n,0,sig)
data <- data.frame(y=y,x=x)
}
noncoverage <- function(n,b0,b1,sig){
dat <- simfun(n,b0,b1,sig)
fit <- lm(y~x,data=dat)
res=summary(fit)
estim = coefficients(res)[,1]
se= coefficients(res)[,2]
ci1 = estim[1] + qnorm(c(.025,.975))*se[1]
nc1 = ifelse((ci1[1]<b0 & ci1[2]>b0),0,1)
ci2 = estim[2] + qnorm(c(.025,.975))*se[2]
nc2 = ifelse((ci2[1]<b1 & ci2[2]>b1),0,1)
nc = data.frame(nc1=nc1,nc2=nc2)
}
set.seed(36)
com=replicate(4,noncoverage(200,1,2,.5))
Code:
> com
, , 1
nc1 nc2
1 0 0
, , 2
nc1 nc2
1 1 0
, , 3
nc1 nc2
1 0 0
, , 4
nc1 nc2
1 0 0
How can I compute the mean by R command ?
Many thanks! Regards.