在R中使用L- l-插入r的函数

发布于 2025-02-09 22:52:43 字数 866 浏览 2 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗲 2025-02-16 22:52:43

我对data.table不太熟悉,因此lapply()调用中的符号看起来很陌生,但是在处理它之后,我认为它正在做什么你打算。

检查它的一种方法是对其进行不同的编码,并在视觉上比较结果:

library(dplyr)
out_list <- list()
for(i in 1:999){
  df2 <- df %>%
    group_by(sp) %>%
    mutate(est = f(x, v)) %>%
    select(sp, est) %>%
    distinct()
  out_list[[i]] <- df2
}

out_df <- bind_rows(out_list, .id = "id")

library(ggplot2)
p1 <- ggplot() + 
  geom_histogram(data = out_df, mapping = aes(x = est, color = sp, fill = sp), show.legend = FALSE) + 
  facet_wrap(~sp)
p2 <- ggplot() +
  geom_histogram(data = result, mapping = aes(x = est, color = sp, fill = sp), show.legend = FALSE) +
  facet_wrap(~sp)

gridExtra::grid.arrange(p1, p2, ncol = 2)

I'm not super familiar with data.table, so the notation in the lapply() 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:

library(dplyr)
out_list <- list()
for(i in 1:999){
  df2 <- df %>%
    group_by(sp) %>%
    mutate(est = f(x, v)) %>%
    select(sp, est) %>%
    distinct()
  out_list[[i]] <- df2
}

out_df <- bind_rows(out_list, .id = "id")

library(ggplot2)
p1 <- ggplot() + 
  geom_histogram(data = out_df, mapping = aes(x = est, color = sp, fill = sp), show.legend = FALSE) + 
  facet_wrap(~sp)
p2 <- ggplot() +
  geom_histogram(data = result, mapping = aes(x = est, color = sp, fill = sp), show.legend = FALSE) +
  facet_wrap(~sp)

gridExtra::grid.arrange(p1, p2, ncol = 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文