【发布时间】:2020-06-18 03:39:01
【问题描述】:
我在使用replicate 重复该功能时遇到问题。
我尝试使用引导程序来适应 一个二次模型,使用浓度作为预测变量,Total_lignin 作为响应,并报告最大值的估计值和相应的标准误差。
我的想法是创建一个名为 bootFun 的函数,它基本上在 for 循环的一次迭代中完成所有事情。 bootFun 只接受了预测变量的数据集和使用的响应(两个变量名都用引号括起来)。
但是,SD 为 0,不正确。不知道哪里错了。你能帮我解决一下吗?
# Load the libraries
library(dplyr)
library(tidyverse)
# Read the .csv and only use M.giganteus and S.ravennae.
dat <- read_csv('concentration.csv') %>%
filter(variety == 'M.giganteus' | variety == 'S.ravennae') %>%
arrange(variety)
# Check the data
head(dat)
# sample size
n <- nrow(dat)
# A function to do one iteration
bootFun <- function(dat, pred, resp){
# Draw the sample size from the dataset
sample <- sample_n(dat, n, replace = TRUE)
# A quadratic model fit
formula <- paste0('resp', '~', 'pred', '+', 'I(pred^2)')
fit <- lm(formula, data = sample)
# Derive the max of the value of concentration
max <- -fit$coefficients[2]/(2*fit$coefficients[3])
return(max)
}
max <- bootFun(dat = dat, pred = 'concentration', resp = 'Total_lignin' )
# Iterated times
N <- 5000
# Use 'replicate' function to do a loop
maxs <- replicate(N, max)
# An estimate of the max of predictor and corresponding SE
mean(maxs)
sd(maxs)
【问题讨论】:
-
欢迎您!请提供
dput(dat),不用说我们无法访问您的 .csv 文件。在此处阅读我们如何提出可重复的问题:stackoverflow.com/questions/5963269/… -
您正在复制 single 函数调用的 结果 N 次,而不是复制函数调用 N 次。
-
maxs <- replicate(N, bootFun (...)) -
看到这个SO post。