我使用R:
获得以下代码x0 <- matrix(rnorm(100,1))
x <- as.matrix(cbind("Intercept"=1, x0))
n <- dim(x0)[[1]]
z <- cbind(rep(1,n),x0)
p <- dim(x0)[[2]]+1
for(i in 1:n) {
gstart <- glm(y~x0,family=binomial)$coef
}
我想计算n
个样本中先前广义线性模型的估计值,并为n
个实例创建估算矩阵,然后计算bias
和mean square error
,其中参数矩阵由以下代码给出:
n=100 #is the number of samples
parameter.mat<-cbind(rep(2,n),rep(2,n))
答案 0 :(得分:1)
我认为 您想要检查glm
返回的系数与平均非参数bootstrap
系数之间的差异。下面的示例首先使用boot package
给出一种方法,然后使用循环(类似于您的问题)
# some example data - set seed for reproducibility
set.seed(1)
dat <- data.frame(y = rbinom(100, 1, 0.5), x = rnorm(100))
# samples
n <- 1000
# glm estimates
mod <- glm(y ~ x, family="binomial", data=dat)$coef
# alternative method using boot package -----------------------------------
library(boot)
# function to extract model coefficients
f <- function(dat, i) glm(y ~ x, family="binomial", data=dat[i, ])$coef
# run bootstrap
set.seed(1)
boot(dat, f, R=n)
# manual bootstrap - sample with replacement -----------------------------
out <- vector("list", length=n)
for(i in 1:n) {
newdat <- dat[sample(1:nrow(dat), , T), ]
out[[i]] <- glm(y ~ x, family="binomial", data=newdat)$coef
}
# matrix of bootstrap coefficients
bc <- do.call("rbind", out)
# bootstrap means
bc.mn <- colMeans(bc)
bias <- mod - bc.mn