我想獲得我在 for 回圈中創建的模型估計,并將它們全部保存在 r 的資料框中。代碼的第一部分只是為了模擬一個類似的資料集來挖掘
library(readxl)
library(dplyr)
library(drc)
library(purrr)
library(readr)
library(ggplot2)
library(broom)
library(broom.mixed)
library(brms)
library(lme4)
########## Simulation #########################################################
#number of assays
nassay= 12
#number of plates
nplate= 3
#fixed mean from above model
mu=0.94587
### mean SD
musd=0.04943
#standard deviation of assay from intial model
sda=0.06260
#standard deviation of residual between plates
sd= 0.07793
set.seed(16)
(assay = rep(LETTERS[1:nassay], each = nplate))
( plate =1:(nassay*nplate) )
plate2 =rep(c(1, 2, 3), times = 12)
### simulate assay level effect
( assayeff = rnorm(nassay, 0, sda) )
#### each assay has 3 plates the assay must be repeated for each plate
( assayeff = rep(assayeff, each = nplate) )
#### every plate measurement has an effect on potency so we draw for every observation based on sd of residual
plateeff= (rnorm(nassay*nplate, 0, sd))
###### simulate different means
(musims= rnorm(nassay*nplate, mu, musd))
( dat = data.frame(assay, assayeff, plate, plateeff,musims) )
sim_dat <- dat
#### now combine all estimates to get rel potency
( dat$relpot = with(dat, mu assayeff plateeff ) )
sim_dat <- dat
fit1 = lmer(relpot ~ 1 (1|assay), data = dat)
fit1
這是模擬資料集的代碼,然后我只是 BRMS 來獲取后驗估計并保存到資料幀
fit<-brms::brm(relpot ~ 1 (1 | Filename), data = dat,iter=100,warmup=10,seed=355545)
post_dat <- posterior_samples(fit,fixed=TRUE)
summary(fit)
plot(fit)
post_fit_use <- post_dat %>% dplyr::select(b_Intercept, sd_Filename__Intercept, sigma)
post_fit_use <- post_fit_use %>% mutate(assay_var=(sd_Filename__Intercept)^2) %>% mutate(platevar=(sigma)^2)
現在我想使用這些后驗估計中的每一個來創建一個資料集并運行一個模型
for (i in 1:nrow(post_fit_use)) { #fixed mean from above model
mu=post_fit_use$b_Intercept[i]
#standard deviation of assay from intial model
sda=post_fit_use$sd_Filename__Intercept[i]
#standard deviation of residual between plates
sd= post_fit_use$sigma[i]
(assay = rep(LETTERS[1:nassay], each = nplate))
( plate =1:(nassay*nplate) )
plate2 =rep(c(1, 2, 3), times = 12)
### simulate assay level effect
( assayeff = rnorm(nassay, 0, sda) )
#### each assay has 3 plates the assay must be repeated for each plate
( assayeff = rep(assayeff, each = nplate) )
#### every plate measurement has an effect on potency so we draw for every observation based on sd of residual
plateeff= (rnorm(nassay*nplate, 0, sd))
###### simulate different means
( dat = data.frame(assay, assayeff, plate, plateeff) )
sim_dat <- dat
#### now combine all estimates to get rel potency
( dat$relpot = with(dat, mu assayeff plateeff ) )
sim_dat <- dat
fit = lmer(relpot ~ 1 (1|assay), data = dat)
rand <-tidy(fit, effects = "ran_pars", scales = "vcov")
fixed <- tidy(fit, effects = "fixed")
}
我的問題是我想將每個模型估計值保存到資料框中。但是當我運行我的回圈時,我只會得到最后一次迭代的結果。我不確定如何保存每個
上面的代碼顯示了我累了,最后一個模型沒有全部保存。請注意,當您運行時,您將 rand <-tidy(fit, effects = "ran_pars", scales = "vcov") fixed <- tidy(fit, effects = "fixed")
獲得具有 1 行 5 個變數的資料框用于固定,以及一個具有 2 行 5 個變數的資料框用于 rand。這是一個模型
uj5u.com熱心網友回復:
我贊同保羅的建議。您可以將結果存盤tidy
在串列中,然后在資料框中轉換串列。但是您將不得不使用雙括號來索引串列(即rands[[i]] <- tidy(fit)
)。嘗試類似:
library(broom)
rands <- list()
for(i in 2:ncol(mtcars)){
mod <- lm(mtcars[,1] ~ mtcars[,i])
rands[[i]] <- tidy(mod)
}
df <- do.call(rbind, rands)
df
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/526658.html
標籤:rfor循环造型brms