如何将摘要函数存储到一个向量中,然后在R中使用for循环?

huangapple go评论71阅读模式
英文:

How to store summary function into a vector then for loop in R?

问题

我有以下的数据集。对于每个月份和站点,我试图使用特定的包(EcoSim)来估计重叠(RA4Model)。

我想要实现两个目标:

install.packages("EcoSimR")
library(EcoSimR)
set.seed(111)
month <- rep(c("J","J","J","F"), each = 4)
site <- rep(c("1","2","3","1"), each = 4)
species <- rep(c("A","B","C","D"), rep = 4)
q1 <- rnorm(16,5,1)
q2 <- rnorm(16,5,1)
q3 <- rnorm(16,5,1)
q4 <- rnorm(16,5,1)
q5 <- rnorm(16,5,1)

df <- data.frame(month, site, species,q1,q2,q3,q4,q5)
df.site <- df[df$month == "J" & df$site == "1",]
df.site <- df.site[,-c(1,2,3)]

RA4model <- niche_null_model(speciesData=df.site, 
                             algo="ra4", metric="pianka", 
                             suppressProg=TRUE,nReps=5000)

summary(RA4model)

首先,我想要将摘要中的某些值存储到R中的相应行中。
以下是月份 "J" 和站点 "1" 的 summary(RA4Model) 的输出:

# 时间戳:  2023年1月8日 11:43:05 
# 可重复性:  FALSE 
# 复制次数:  5000 
# 经过的时间:  1秒 
# 度量:  pianka 
# 算法:  ra4 
# 观察指数:  0.96516 
# 模拟指数的均值:  0.95101 
# 模拟指数的方差:  9.4906e-05 
# 较低95%(1尾):  0.93823 
# 较高95%(1尾):  0.96979 
# 较低95%(2尾):  0.93729 
# 较高95%(2尾):  0.97272 
# 较低尾部P =  0.8982 
# 较高尾部P =  0.1018 
# 观察到的度量 > 4491个模拟度量 
# 观察到的度量 < 509个模拟度量 
# 观察到的度量 = 0个模拟度量 
# 标准化效应大小(SES):  1.453 

将此输出存储到向量中。我不知道如何将摘要输出中的 lower-1tailP 和 SES 存储到数据集中:

df.out <- df[,c(1,2,3)]
df.out$Obs <- RA4model$Obs
df.out$Sim <- mean(RA4model$Sim)
df.out$lower-1tailP <-  # 这应该是0.93
df.out$SES <-  # 这应该是1.453

接下来,我想要循环执行这个操作,以便对每个唯一的(month,site) 进行操作。因此,最终的数据框看起来类似于这样:

month site Obs Sim lower-1tailP SES
  J     1   ..  ..   ..          ..
  J     2   ..   ..  ..          ..
  J     3   ..   ..  ..          ..
  F     1   ..   ..  ..          ..
英文:

I have the following dataset. For each month and site, I'm trying to use a particular package (EcoSim) to estimate overlap (RA4Model).

I want to achieve two things:

install.packages(&quot;EcoSimR&quot;)
library(EcoSimR)
set.seed(111)
month &lt;- rep(c(&quot;J&quot;,&quot;J&quot;,&quot;J&quot;,&quot;F&quot;), each = 4)
site &lt;- rep(c(&quot;1&quot;,&quot;2&quot;,&quot;3&quot;,&quot;1&quot;), each = 4)
species &lt;- rep(c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;), rep = 4)
q1 &lt;- rnorm(16,5,1)
q2 &lt;- rnorm(16,5,1)
q3 &lt;- rnorm(16,5,1)
q4 &lt;- rnorm(16,5,1)
q5 &lt;- rnorm(16,5,1)

df &lt;- data.frame(month, site, species,q1,q2,q3,q4,q5)
df.site &lt;- df[df$month == &quot;J&quot; &amp; df$site == &quot;1&quot;,]
df.site &lt;- df.site[,-c(1,2,3)]

RA4model &lt;- niche_null_model(speciesData=df.site, 
                             algo=&quot;ra4&quot;, metric=&quot;pianka&quot;, 
                             suppressProg=TRUE,nReps=5000)

summary(RA4model)

First, I want to store certain values from the summary into the corresponding rows in R
Here is the output of summary(RA4Model) for Month "J" and site "1"

# Time Stamp:  Sun Jan  8 11:43:05 2023 
# Reproducible:  FALSE 
# Number of Replications:  5000 
# Elapsed Time:  1 secs 
# Metric:  pianka 
# Algorithm:  ra4 
# Observed Index:  0.96516 
# Mean Of Simulated Index:  0.95101 
# Variance Of Simulated Index:  9.4906e-05 
# Lower 95% (1-tail):  0.93823 
# Upper 95% (1-tail):  0.96979 
# Lower 95% (2-tail):  0.93729 
# Upper 95% (2-tail):  0.97272 
# Lower-tail P =  0.8982 
# Upper-tail P =  0.1018 
# Observed metric &gt; 4491 simulated metrics 
# Observed metric &lt; 509 simulated metrics 
# Observed metric = 0 simulated metrics 
# Standardized Effect Size (SES):  1.453 

Storing this output into vector. I don't know how to store the lower-1tailP and SES from the summary output into the dataset

df.out &lt;- df[,c(1,2,3)]
df.out$Obs &lt;- RA4model$Obs
df.out$Sim &lt;- mean(RA4model$Sim)
df.out$lower-1tailP &lt;-  #This should be 0.93
df.out$SES &lt;-  #This should be 1.453

Next, I want to loop this so that it does it for each unique(month,site). So the final dataframe looks something like this:

month site Obs Sim lower-1tailP SES
  J     1   ..  ..   ..          ..
  J     2   ..   ..  ..          ..
  J     3   ..   ..  ..          ..
  F     1   ..   ..  ..          ..

答案1

得分: 2

您可以创建一个名为get_eco_sim_result()的辅助函数,它返回感兴趣的参数列表:

get_eco_sim_result <- function(spd, algo = "ra4", metric = "pianka", nReps = 500) {
  model = niche_null_model(speciesData = spd,
    algo = algo, metric = metric, nReps = nReps, suppressProg = TRUE
  )
  return(list(
    Obs = model$Obs,
    Sim = mean(model$Sim),
    lower_1tailp = quantile(model$Sim, 0.05),
    SES = (model$Obs - mean(model$Sim)) / sd(model$Sim)
  ))
}

然后使用lapply()来将该辅助函数应用于数据的每个子集;在这里,我使用split()获取数据的子集。通过将返回的列表包装在data.frame()中,您随后可以使用do.call()rbind()一起使用:

do.call(
  rbind, lapply(split(df, list(month, site), drop = TRUE), function(d) {
    data.frame(get_eco_sim_result(d[, -c(1, 2, 3)], nReps = 5000))
  })
)

输出:

          Obs       Sim lower_1tailp       SES
F.1 0.9641760 0.9546306    0.9429722 1.0966176
J.1 0.9651613 0.9508969    0.9381335 1.4635265
J.2 0.9931026 0.9842322    0.9800247 2.9524328
J.3 0.9726413 0.9674799    0.9586858 0.7669562

不过,您最初的问题可能更多地与辅助函数和循环无关,而是与summary(RA4model)中的参数是如何估计的有关?使用getAnywhere(summary.nichenullmod)来查看这些参数是如何估计的。

英文:

You can create a helper function get_eco_sim_result(), which returns a list of the parameters of interest:

get_eco_sim_result &lt;- function(spd, algo= &quot;ra4&quot;, metric = &quot;pianka&quot;, nReps=500) {
  model = niche_null_model(speciesData = spd,
    algo = algo,metric =metric, nReps = nReps, suppressProg = TRUE
  )
  return(list(
    Obs = model$Obs,
    Sim = mean(model$Sim),
    lower_1tailp = quantile(model$Sim,0.05),
    SES = (model$Obs - mean(model$Sim))/sd(model$Sim)
  ))
}

Then use lapply() to apply that helper function to each subset of the data; here I obtain the subsets of the data using split(). By wrapping the retuned list in data.frame(), you can subsequently use do.call() with rbind()

do.call(
  rbind, lapply(split(df, list(month,site), drop=T), \(d) {
    data.frame(get_eco_sim_result(d[,-c(1,2,3)], nReps=5000))
  })
)

Output:

          Obs       Sim lower_1tailp       SES
F.1 0.9641760 0.9546306    0.9429722 1.0966176
J.1 0.9651613 0.9508969    0.9381335 1.4635265
J.2 0.9931026 0.9842322    0.9800247 2.9524328
J.3 0.9726413 0.9674799    0.9586858 0.7669562

Your original question, however, is perhaps less about the helper function and the loop, but rather about how the parameters in summary(RA4model) are estimated? Use getAnywhere(summary.nichenullmod) to see how these are estimated.

huangapple
  • 本文由 发表于 2023年1月9日 03:56:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定