我需要帮助执行两个模型的bootstrap

时间:2016-04-14 20:47:50

标签: r regression statistics-bootstrap

我想执行一个引导程序来更好地估计beta估计值,使用simex方法进行模拟。

基本代码如下。应重复Value1和Value2。

library(simex)
library(data.table) 

value1 <- rnorm(24, mean = 1, sd = 0.3) 
value2 <- rnorm(24,mean = 1.41, sd = 0.5)
group1 <- as.data.frame(value1)
group1$Treat <- 1
setnames(group1, "value1", "Value") 
group2 <-  as.data.frame(value2)
group2$Treat <- 2
setnames(group2, "value2", "Value") 
simulated_data <- rbind(group1, group2)

naive_model <- lm(Treat~Value, data = simulated_data, x = T, y = T)
simex_simulated_data <- simex(naive_model, SIMEXvariable="Value", measurement.error = 0.12, B=100, fitting.method="quadratic")

编辑:

这是我走了多远,不远。似乎这个函数不像x = T,但它需要Simex。

> getRegrnaive <- function(dat, idx) {   bsFit <- lm(Value~Treat,
> subset=idx, data=dat)   coef(bsFit) }
> 
> getRegr <- function(dat, idx) {   bsFit <- lm(Value~Treat, subset=idx,
> data=dat, x = T, y = T)   simex_boot <- simex(bsFit,
> SIMEXvariable="Value", measurement.error = 0.12, B=100,
> fitting.method="quadratic")   coef(simex_boot) }
> 
> #------------------------ 
>nR <- 999 
>bsRegrsim <- boot(simulated_data, statistic=getRegr, R=nR) 
> #---------------------------------------- 
>bsRegrnaiv <- boot(simulated_data, statistic=getRegrnaive, R=nR) 

或类似的东西?

  

getRegr&lt; - function(simulated_data,indices){d&lt; -   simulated_data [indices,] bsFit&lt; - lm(Value~Treat,data = d,x = T,y =   T)simex_boot&lt; - simex(bsFit,SIMEXvariable =“Value”,   measurement.error = 0.12,B = 100,fitting.method =“quadratic”)
  coef(simex_boot)} summary(simex_boot)

然而,它没有做我想要的......问题(导致结果的可变性)是我想要复制的最初的瞳孔....

所以, 我这样做了:

f <- function (n=8) {
  sderrormanual <- 0.12
  sderrormacro <- 0.02
  sdmeasuredtreatment <- 0.3
  sdmeasuredcontrol <- 0.5
  #n <-8
  errormanual <- rnorm (n*2, mean = 0, sd = sderrormanual) 
  errormacro <- rnorm (n*2, mean = 0, sd = sderrormacro) 
  value1 <- rnorm(n, mean = 1, sd = 0.18) #normalverteilung anhand der system. Analyse von Felix, ausgehend von einem errechneten, "wahren" SD
  value2 <- rnorm(n, mean = 14.1, sd = 0.38)#normalverteilung anhand der system. Analyse von Felix, ausgehend von einem errechneten, "wahren" SD
  group1 <- as.data.frame(value1)
  group1$Treat <- 1
  setnames(group1, "value1", "Value") 
  group2 <-  as.data.frame(value2)
  group2$Treat <- 2
  setnames(group2, "value2", "Value") 
  simulated_data <- rbind(group1, group2)
  simulated_data$errormanual <- errormanual 
  simulated_data$errormacro <- errormacro 
  simulated_data$Valueerrormanual <- simulated_data$Value + simulated_data$errormanual
  simulated_data$Valueerrormacro <- simulated_data$Value + simulated_data$errormacro
  d <- simulated_data
  bsFit <- lm(Valueerrormanual~Treat, data=d)
}

但不是

out <- replicate(10, f(), simplify = "array")

导致一些疯狂的估计,与

的原始结果没有任何关系
  

bsFit&lt; - lm(Valueerrormanual~Treat,data = d)

我不明白

呃...这是一个错字

1 个答案:

答案 0 :(得分:0)

getRegr <- function(simulated_data, indices) {
  d <- simulated_data[indices,]
  bsFit <- lm(Value~Treat, data=d, x = T, y = T)
  simex_boot <- simex(bsFit, SIMEXvariable="Value", measurement.error = 0.12, B=100, fitting.method="quadratic")
  coef(simex_boot)
}
summary(simex_boot)