2016-04-14 40 views
0

我想執行引導,以獲得更好的貝塔估計估計,用simex方法模擬。我需要幫助執行與兩個模型引導

基本代碼如下。值1和值2應該重複。

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 < - 函數(simulated_data,索引){d < - simulated_data [指數] bsFit < - LM(價值〜治療,數據= d,X = T,Y = T)simex_boot < - SIMEX(bsFit,SIMEXvariable = 「值」, measurement.error = 0.12,B = 100,fitting.method = 「二次」)
COEF(simex_boot)}摘要(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 < -lm(Valueerrormanual_Treatment,data = d)

我不`噸明白

嗯...這是一個錯字

+0

你只是想引導新的數據和擬合模型,然後取平均估算? – adaien

回答

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)