英文:
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 <- 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)
#function to estimate model coefficients
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 models for each species
result <- rbindlist(
lapply(1:999, \(i) df[, .(rand.cor = f(x, v), x, v), sp][, i:=i])
)
答案1
得分: 1
问题在于当前的函数f
只返回rand.corr
向量。所以,如果我们想要获取在f
中定义的其他向量(即v.sample
和y.sample
),那么我们需要添加一行代码,将所有向量返回到一个数据框中,然后与x
、v
、sp
和i
一起连接到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 result
because 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 <- 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")
# storing all vectors in a dataframe
data.frame( v.sample = v.sample, y.sample = y.sample, rand.cor = per)
}
result <- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论