如何在R中打印循环输出的参数?

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

How to print parameters of an output from a loop in R?

问题

使用数据集df,我从各种与x相关联的v值中抽取样本以生成随机的J(如函数中所示)。该随机化的输出存储在results中。除了rand.cor = f(x,v)之外,我还希望该表生成用于运行的相关x、v以及生成的y.sample。我尝试添加ax,v,但注意到它只是重复原始x和v中的向量,而不是随机化的向量。

set.seed(333)
J <- rnorm(40, 5, 1)
x <- rnorm(40, 2, 1)
v <- rnorm(40, 10, 1)
sp <- rep(c("A", "B", "C", "D"), each=10)

df <- data.frame(J, x, v, sp)

library(data.table)
setDT(df)
# 用于估计模型系数的函数
f <- function(x, v) {
  v.sample <- sample(v, length(v), replace=TRUE)
  y.sample <- (v.sample/x^2) - (1/x)
  per <- cor(y.sample, x, use="complete.obs")
}


# 对每个物种进行999次模型估计
result <- rbindlist(
  lapply(1:999, \(i) df[, .(rand.cor = f(x, v), x, v), sp][, i:=i])
)
英文:

Using the dataset df, I sample from various values of v coupled with x to generate a randomized J (as shown in the function). The output of that randomization is stored is results. Along with rand.cor = f(x,v), I also want the table to generate the associated x, v that it picked for the run and and the y.sample generated. I tried adding ax,v but I noticed that it was simply repeating the vector in original x and v, not the randomized ones.

set.seed(333)
J &lt;- rnorm(40, 5, 1)
x &lt;- rnorm(40, 2, 1)
v &lt;- rnorm(40, 10, 1)
sp &lt;- rep(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;), each=10)

df &lt;- data.frame(J, x, v, sp)


library(data.table)
setDT(df)
#function to estimate model coefficients
f &lt;- function(x, v) {
  v.sample &lt;- sample(v, length(v), replace=TRUE)
  y.sample &lt;- (v.sample/x^2) - (1/x)
  per &lt;- cor(y.sample, x, use=&quot;complete.obs&quot;)
}
    

# 999 models for each species
result &lt;- rbindlist(
  lapply(1:999, \(i) df[, .(rand.cor = f(x, v), x, v), sp][, i:=i])
)

答案1

得分: 1

问题在于当前的函数f只返回rand.corr向量。所以,如果我们想要获取在f中定义的其他向量(即v.sampley.sample),那么我们需要添加一行代码,将所有向量返回到一个数据框中,然后与xvspi一起连接到result中。因为在当前的结果中,只有rand.corr会被存储,所以我们需要从lapply()函数中移除rand.corr=来获取从f返回的所有向量:

set.seed(333)
J <- rnorm(40, 5, 1)
x <- rnorm(40, 2, 1)
v <- rnorm(40, 10, 1)
sp <- rep(c("A", "B", "C", "D"), each=10)

df <- data.frame(J, x, v, sp)

library(data.table)
setDT(df)

f <- function(x, v) {
  v.sample <- sample(v, length(v), replace=TRUE)
  y.sample <- (v.sample/x^2) - (1/x)
  per <- cor(y.sample, x, use="complete.obs")
  # 将所有向量存储在数据框中
  data.frame( v.sample = v.sample, y.sample = y.sample, rand.cor = per)
}

result <- rbindlist(
  lapply(1:999, function(i)  df[, .(f(x, v),x,v, sp)][, i:=i])
)

head(result) # 一个包含39960行的数据框

希望这是你要找的内容。

英文:

The issue is that the current function f returns rand.corr vector only. So if we would like to get other vectors defined in f (i.e, v.sample, y.sample) then we need to add one more line returning all vectors into a dataframe and then joining it with x, v, sp, and i in the resultbecause in the current result, only rand.corr would be stored, so we need to remove the rand.corr= from the lapply() function to get all vectors returned from f:

set.seed(333)
J &lt;- rnorm(40, 5, 1)
x &lt;- rnorm(40, 2, 1)
v &lt;- rnorm(40, 10, 1)
sp &lt;- rep(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;), each=10)

df &lt;- data.frame(J, x, v, sp)

library(data.table)
setDT(df)

f &lt;- function(x, v) {
  v.sample &lt;- sample(v, length(v), replace=TRUE)
  y.sample &lt;- (v.sample/x^2) - (1/x)
  per &lt;- cor(y.sample, x, use=&quot;complete.obs&quot;)
# storing all vectors in a dataframe
  data.frame( v.sample = v.sample, y.sample = y.sample, rand.cor = per)
}

result &lt;- rbindlist(
  lapply(1:999, \(i)  df[, .(f(x, v),x,v, sp)][, i:=i])
)

head(result) # a dataframe with 39960 rows

   v.sample   y.sample   rand.cor         x         v sp i
1: 10.515567  1.9575322 -0.3232905 2.0763333 10.419046  A 1
2: 10.159445  2.6790438 -0.3232905 1.7696440 10.920101  A 1
3:  9.581106  1.3524713 -0.3232905 2.3174648  8.802001  A 1
4: 10.164441 58.8111686 -0.3232905 0.4073156  8.568937  A 1
5: 10.948986  2.7624775 -0.3232905 1.8180594  9.581106  A 1
6: 10.164441  0.4542123 -0.3232905 3.7561417 10.233590  A 1

Hope thats what you are looking for.

huangapple
  • 本文由 发表于 2023年3月3日 19:15:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626392.html
匿名

发表评论

匿名网友

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

确定