计算glm系数矩阵,偏差和均方误差

时间:2015-01-23 23:22:18

标签: r matrix glm

我使用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个实例创建估算矩阵,然后计算biasmean square error,其中参数矩阵由以下代码给出:

n=100 #is the number of samples
parameter.mat<-cbind(rep(2,n),rep(2,n))  

1 个答案:

答案 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