gpt4 book ai didi

r - 从 dplyr 管道创建预测网格

转载 作者:行者123 更新时间:2023-12-04 11:39:08 26 4
gpt4 key购买 nike

我希望有人有解决方案,可以在使用 dplyr 的管道中使用某种形式的 expand.grid。我正在做一些建模,其中我有几个不同的组(或下面的类型)并且这些组具有不同的 x 和 y 数据范围。一旦我对数据运行游戏,我就有兴趣为预测创建一个图,但我只想预测每个值占据的范围内的值,而不是数据集的整个范围。

我已经在下面发布了一个工作示例,但我想知道是否有办法绕过循环并完成我的任务。

干杯

require(ggplot2)
require(dplyr)

# Create some data
df = data.frame(Type = rep(c("A","B"), each = 100),
x = c(rnorm(100, 0, 1), rnorm(100, 2, 1)),
y = c(rnorm(100, 0, 1), rnorm(100, 2, 1)))

# and if you want to check out the data
ggplot(df,aes(x,y,col=Type)) + geom_point() + stat_ellipse()

# OK so I have no issue extracting the minimum and maximum values
# for each type
df_summ = df %>%
group_by(Type) %>%
summarize(xmin = min(x),
xmax = max(x),
ymin = min(y),
ymax = max(y))
df_summ

# and I can create a loop and use the expand.grid function to get my
# desired output
test = NULL
for(ii in c("A","B")){
df1 = df_summ[df_summ$Type == ii,]
x = seq(df1$xmin, df1$xmax, length.out = 10)
y = seq(df1$ymin, df1$ymax, length.out = 10)
coords = expand.grid(x = x, y = y)
coords$Type = ii
test = rbind(test, coords)
}

ggplot(test, aes(x,y,col = Type)) + geom_point()

但我真正想做的是找到绕过循环的方法并尝试直接从我的管道运算符(operator)那里获得相同的输出。我尝试了一些使用 do() 函数的组合,但没有效果,而下面发布的只是许多失败尝试中的一个

df %>%
group_by(Type) %>%
summarize(xmin = min(x),
xmax = max(x),
ymin = min(y),
ymax = max(y)) %>%
do(data.frame(x = seq(xmin, xmax, length.out = 10),
y = seq(ymin, ymax, length.out = 10)))

# this last line returns an error
# Error in is.finite(from) :
# default method not implemented for type 'closure'

最佳答案

您的do() 尝试几乎是正确的。诀窍是在总结之后重新分组(这似乎放弃了分组)。您还需要确保使用 .$ 从链中的数据中获取值。试试这个

test <- df %>%
group_by(Type) %>%
summarize(xmin = min(x),
xmax = max(x),
ymin = min(y),
ymax = max(y)) %>%
group_by(Type) %>%
do(expand.grid(x = seq(.$xmin, .$xmax, length.out = 10),
y = seq(.$ymin, .$ymax, length.out = 10)))
ggplot(test, aes(x,y,col = Type)) + geom_point()

enter image description here

关于r - 从 dplyr 管道创建预测网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43124061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com