gpt4 book ai didi

r - 如何将 gam() 合并到 Lattice 包的 xyplot() 中?

转载 作者:行者123 更新时间:2023-12-04 02:24:33 25 4
gpt4 key购买 nike

我正在尝试将 mgcv 包中的广义加性模型 gam() 合并到 xyplot() 函数或 coplot( ) R 中 lattice 包中的函数。

数据可以在http://statweb.stanford.edu/~tibs/ElemStatLearn/中找到, 通过选择臭氧数据。

这是我的内核平滑代码。

    ozonedata=ozone 
Temperature=equal.count(ozonedata$temperature,4,1/2)
Wind=equal.count(ozonedata$wind,4,1/2)

xyplot(ozone^(1/3) ~ radiation | Temperature * Wind, data = ozonedata, as.table = TRUE,
panel = function(x, y, ...) {panel.xyplot(x, y, ...);panel.loess(x, y)},
pch = 20,xlab = "Solar Radiation", ylab = "Ozone (ppb)")

    coplot((ozone^(1/3))~radiation|temperature*wind,data=ozonedata,number=c(4,4),
panel = function(x, y, ...) panel.smooth(x, y, span = .8, ...),
xlab="Solar radiation (langleys)", ylab="Ozone (cube root ppb)")

生成广义加性模型如下。

    gam_ozone = gam(ozone^(1/3)~s(radiation)+s(temperature)+s(wind),data=ozonedata,method="GCV.Cp")

现在我无法将 gam() 的拟合结合到点阵图中。

最佳答案

我认为这应该可行。请注意,我们将 gam_ozone 对象作为参数传递给名为 gamxyplot。这将允许我们在面板功能中访问它。

xyplot(ozone^(1/3) ~ radiation | Temperature * Wind, data = ozonedata, 
as.table = TRUE, gam=gam_ozone,
panel = function(x, y, gam,...) {

xx <- dimnames(trellis.last.object())
ww <- arrayInd(packet.number(), .dim=sapply(xx, length))
avgtmp <- mean(xx[[1]][[ww[1]]])
avgwind <- mean(xx[[2]][[ww[2]]])

gx<-seq(min(x, na.rm=T), max(x, na.rm=T), length.out=50)
gy<-predict(gam, data.frame(radiation=gx,
temperature=avgtmp, wind=avgwind))

panel.xyplot(x, y, ...);
panel.xyplot(gx, gy, ..., col="red", type="l");
},
pch = 20,xlab = "Solar Radiation", ylab = "Ozone (ppb)"
)

现在,为了使用 gam 进行预测,您必须为每个面板找到用于风和温度的值。我决定做的,只是取每个带状疱疹范围的中间值。所以我用了 Deepayan-approved, undocumented feature通过调用 dimnames 获取每个当前带状疱疹的范围。然后我用 packet.number() 找到当前面板 一旦我有了范围,我就采取方法获得平均值。

不,我将使用我们传入的 gam 模型来预测每个面板的曲线。我根据观察值计算了 50 个 x 值的范围,然后从 gam 中预测了一个新行。

最后,我使用 panel.xyplot 绘制原始数据,然后绘制 gam 预测线。

sample lattice output with gam smooth curve

关于r - 如何将 gam() 合并到 Lattice 包的 xyplot() 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23977949/

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