在R中使用L- l-插入r的函数
在dataframe df
中,我构造了一个函数f
,该函数可以计算x.sample和y.sample之间的相关性。然后,我正在尝试运行999个随机化,以计算中中每个随机化的预期相关性。我不确定此处的lapply是否正确编写,并且是否实际上是按照函数的
。给定我在4
的sp
上计算没有任何其他功能的简便方法是什么?
set.seed(111)
library(truncnorm)
x <- rtruncnorm(n = 288,a = 0,b = 10,mean = 5,sd = 2)
v <- rtruncnorm(n = 288,a = 0,b = 10,mean = 5,sd = 2)
y <- ((v/x^2) - (1/x))
sp <- rep(c("A","B","C","D"), each = 72)
df <- data.frame(v,x,y,sp)
library(data.table)
setDT(df)
# function to estimate model coefficients
f <- function(x,v) {x.sample <- sample(x, length(x), replace=T)
y.sample <- (v/x.sample^2) - (1/x.sample)
per <- cor(y.sample, x.sample)}
set.seed(1234)
# 999 models for each species
result = rbindlist(
lapply(1:999, \(i) df[,.(est = f(x,v)), sp][, i:=i])
)
In the dataframe df
, I construct a have a function f
that calculates the correlation between x.sample and y.sample. Then, I am trying to run 999 randomizations that calculates the expected correlation for each randomization in per
. I am not sure if the lapply here is written correctly and if it's actually taking in the per
function. What is an easy way to verify this given that I'm calculating per
and not any other function over the 4 sp
?
set.seed(111)
library(truncnorm)
x <- rtruncnorm(n = 288,a = 0,b = 10,mean = 5,sd = 2)
v <- rtruncnorm(n = 288,a = 0,b = 10,mean = 5,sd = 2)
y <- ((v/x^2) - (1/x))
sp <- rep(c("A","B","C","D"), each = 72)
df <- data.frame(v,x,y,sp)
library(data.table)
setDT(df)
# function to estimate model coefficients
f <- function(x,v) {x.sample <- sample(x, length(x), replace=T)
y.sample <- (v/x.sample^2) - (1/x.sample)
per <- cor(y.sample, x.sample)}
set.seed(1234)
# 999 models for each species
result = rbindlist(
lapply(1:999, \(i) df[,.(est = f(x,v)), sp][, i:=i])
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对
data.table
不太熟悉,因此lapply()
调用中的符号看起来很陌生,但是在处理它之后,我认为它正在做什么你打算。检查它的一种方法是对其进行不同的编码,并在视觉上比较结果:
I'm not super familiar with
data.table
, so the notation in thelapply()
call looks foreign, but after playing around with it, I think it is doing what you intend.One way to check is to code it differently and visually compare results: