- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想通过 gam
结果用 ggplot2
绘制等高线图。下面是我想要的详细解释:
#packages
library(mgcv)
library(ggplot2)
library(tidyr)
#prepare data
df <- data.frame(x = iris$Sepal.Width,
y = iris$Sepal.Length,
z = iris$Petal.Length)
#fit gam
gam_fit <- gam(z ~
s(x) +
s(y),
data=df,na.action = "na.fail")
为了根据 gam_fit
预测 z 值,我从 https://drmowinckels.io/blog/2019-11-16-plotting-gamm-interactions-with-ggplot2/ 找到了一种方法
#predict z values
df_pred <- expand_grid(
x = seq(from=min(df$x),
to=max(df$x),
length.out = 100),
y = seq(from=min(df$y),
to=max(df$y),
length.out = 100)
)
df_pred <- predict(gam_fit, newdata = df_pred,
se.fit = TRUE) %>%
as_tibble() %>%
cbind(df_pred)
gg <- ggplot() +
geom_tile(data=df_pred, aes(x=x, y=y, fill = fit)) +
geom_point(data=df,aes(x=x, y=y))+
scale_fill_distiller(palette = "YlGnBu")+
geom_contour(data=df_pred, aes(x=x, y=y, z = fit), colour = "white")
print(gg)
我的目标是移除没有测量 x-y 点的平铺和轮廓。例如,绘图的右上角和左上角周围没有测量点。
我想知道 mgcViz
是否可以实现这一点,但它需要包括 x 和 y 作为交互项,如下所示(我也不确定如何在下图中添加测量点):
library(mgcViz)
gamm_fit2 <- gam(z ~
s(x,y),
data=df,na.action = "na.fail") #,REML=TRUE
b <- getViz(gamm_fit2)
plot(sm(b, 1))
我认为 df_pred
可能不是实现我目标的最佳格式,但我不确定该怎么做。如果您能给我任何关于 ggplot2
的解决方案,我将不胜感激。
最佳答案
可能有一个包被设计用来处理这个任务,但是如果你找不到合适的“工具”来完成这项工作,一个选择是围绕“点”绘制一个多边形并将多边形之外的所有东西都涂成灰色,例如
library(tidyverse)
library(mgcv)
#prepare data
df <- data.frame(x = iris$Sepal.Width,
y = iris$Sepal.Length,
z = iris$Petal.Length)
#fit gam
gam_fit <- gam(z ~
s(x) +
s(y),
data=df,na.action = "na.fail")
df_pred <- expand_grid(
x = seq(from=min(df$x),
to=max(df$x),
length.out = 100),
y = seq(from=min(df$y),
to=max(df$y),
length.out = 100)
)
df_pred <- predict(gam_fit, newdata = df_pred,
se.fit = TRUE) %>%
as_tibble() %>%
cbind(df_pred)
ggplot() +
geom_tile(data=df_pred, aes(x=x, y=y, fill = fit)) +
geom_point(data=df,aes(x=x, y=y))+
scale_fill_distiller(palette = "YlGnBu")+
geom_contour(data=df_pred, aes(x=x, y=y, z = fit), colour = "white") +
coord_cartesian(xlim = c(1.9, 4.5),
ylim = c(4, 8))
# Get the 'hull' around all of the dots
hulls <- df[chull(df$x, df$y), ]
# Get the 'edges' of the frame, starting at the first hull point
edges <- data.frame(x = c(4.1,4.5,4.5,1.9,1.9,4.5),
y = c(5.2,4,8,8,4,4),
z = NA)
# Combine
draw_poly <- rbind(hulls, edges)
# Draw the plot, and overlay the gray polygon
ggplot() +
geom_tile(data=df_pred, aes(x=x, y=y, fill = fit)) +
geom_point(data=df, aes(x=x, y=y)) +
scale_fill_distiller(palette = "YlGnBu") +
geom_contour(data=df_pred, aes(x=x, y=y, z = fit), colour = "white") +
geom_polygon(data=draw_poly, aes(x=x, y=y), fill = "grey")
# Without the points
ggplot() +
geom_tile(data=df_pred, aes(x=x, y=y, fill = fit)) +
# geom_point(data=df, aes(x=x, y=y)) +
scale_fill_distiller(palette = "YlGnBu") +
geom_contour(data=df_pred, aes(x=x, y=y, z = fit), colour = "white") +
geom_polygon(data=draw_poly, aes(x=x, y=y), fill = "grey")
由 reprex package 创建于 2022-09-16 (v2.0.1)
这是使用 concaveman package 的另一个示例计算凹包:
library(ggforce)
#install.packages("concaveman")
library(concaveman)
border <- concaveman(as.matrix(df[,1:2]), concavity = 2)
edges <- data.frame(V1 = c(4.5,4.5,1.9,1.9,4.5),
V2 = c(4,8,8,4,4))
draw_poly <- rbind(border, edges)
ggplot() +
geom_tile(data=df_pred, aes(x=x, y=y, fill = fit)) +
geom_point(data=df, aes(x=x, y=y)) +
scale_fill_distiller(palette = "YlGnBu") +
geom_contour(data=df_pred, aes(x=x, y=y, z = fit), colour = "white") +
geom_shape(data=draw_poly, aes(x=V1, y=V2), fill = "grey",
expand = unit(-0.05, "cm"))
ggplot() +
geom_tile(data=df_pred, aes(x=x, y=y, fill = fit)) +
# geom_point(data=df, aes(x=x, y=y)) +
scale_fill_distiller(palette = "YlGnBu") +
geom_contour(data=df_pred, aes(x=x, y=y, z = fit), colour = "white") +
geom_shape(data=draw_poly, aes(x=V1, y=V2), fill = "grey",
expand = unit(-0.05, "cm"))
由 reprex package 创建于 2022-09-16 (v2.0.1)
关于r - 如何使用等高线图和瓦片图可视化 GAM 结果(使用 ggplot2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73738521/
我想在 GAMS 模型中找到我的错误。我没有任何错误,但我的模型效果不佳 GAMS 中是否有任何调试工具?(如其他软件中的调试器工具,例如 MATLAB) 最好的事物 最佳答案 不幸的是,我没有遇到任
我正在使用 GAM 对逻辑回归中的时间趋势进行建模。然而,我想从中提取拟合样条线以将其添加到另一个无法在 GAM 或 GAMM 中拟合的模型中。 因此我有两个问题: 我怎样才能随着时间的推移拟合更平滑
我在 ggplot2 中使用 stat_smooth 函数,决定我想要“拟合优度”,并为此使用了 mgvc gam。我突然想到我应该检查以确保它们是相同的模型(stat_smooth vs mgvc
我在大型数据集上估算一个非常简单的模型。公式看起来像 scam::scam(formula = ratio ~ s(rate,bs="mpi")) 然后使用这些模型为新数据生成预测。我不关心模型的任
我的数据框看起来像: head(bush_status) distance status count 0 endemic 844 1 exotic 8
我正在对过去大约 40 年中零星收集的物种计数数据与一系列环境预测因子进行建模。目前,我的 GAM 是这样的: k = gam(CountIndividuals ~ s(Date, bs = 'cr'
我使用 gam 拟合了广义加性模型来自 mgcv包裹。我有一个数据表,其中包含我的因变量 Y , 自变量 X , 其他自变量 Oth和一个两级因子 Fac .我想适合以下型号 Y ~ s(X) + O
我在 GMAS 中编写我的 MIP 模型,求解器表明我的模型中有 1535272 行、3287490 列和 8425140 个非零(我不确定它对于 GAMS 来说是否太大)。经过 899677 次迭代
我的数据集有许多冗余观测值(但每个观测值都应该被计数)。因此我考虑在 GAM 中使用“权重”选项,因为它可以显着减少计算时间。 gam函数(在 mgcv 包中)解释说它们是“等价的”(来自参数 ?ga
我无法理解为什么收到此错误。我的两个变量都是数字且长度相同,当变量不相同时,我用NA调整数据。但是我仍然收到一个错误,我的响应变量超出范围 year 0))>=5) # If at least
我有3套,我想知道哪个元素不属于 对称差集。 Set1={1*125} 组2={20*450} Set3={45*235} 我用 SymAB 显示了 setA 和 set B 的对称差异。 我计算 s
我不明白为什么下面的两个 gam 模型会产生不同的结果。唯一的区别是在其中一个模型中,我在函数 gam 和 s 之前添加了命名空间说明符 gam::。 我想这样做是因为我正在探索在 gam 包和 mg
我正在对涉及两个拟合步骤的物种分布数据进行障碍类型分析。第一步是使用 family=quasibinomial 的所有数据对 (m1) 存在/不存在数据建模。第二步 (m2) 是使用 family=G
问题移至 CrossValidated 我试图表达 gam 中两个类别之间“增长速度”的差异造型。我的数据表示随着时间的推移 [0-100%] 的累积值,但我希望(为了与其他研究的可比性)以年度值来表
我有一个 gam我所知道的模型在 R 中运行良好,但是当我尝试“train ”使用 caret 相同的模型时package 它返回一个错误,指出输入数据列是列表。有没有人明白这一点? 我正在运行的代码
我有一个非常简单的时间序列数据集,由单个变量(“AVERAGE”)的年平均值组成。我希望研究时间序列的“趋势”分量的变化率(一阶导数)和加速度(二阶导数)以及相关的标准误差。我使用MGCV的GAM和P
我使用 gam 在负二项式族中拟合广义加性模型来自 mgcv包裹。我有一个包含因变量 y 的数据框, 自变量 x , 一个因素 fac和一个随机变量 ran .我适合以下模型 gam1 sum(r
我正在阅读“R 中的应用程序统计学习简介”(ISLR),我被困在第 295 页的一部分,即广义加法模型实验室。当我运行以下代码时,我得到一个错误 Error in plot.gam(gam1, se
我正在研究一个模型,其中包含多个 RE 和一个变量的样条,因此我尝试使用 gam() .但是,我遇到了内存耗尽限制错误(即使我在具有 128GB 的集群上运行它时也是如此)。即使我只用一个 RE
从这个数据: UQdata MudUQ Estuary Site 7.00 10.9 NoriPau A 6.00 13.9 NoriPau A 5.00
我是一名优秀的程序员,十分优秀!