【问题标题】:Export results from LOESS plot从 LOESS 图导出结果
【发布时间】:2021-08-25 21:16:47
【问题描述】:

我正在尝试从 LOESS 图中导出基础数据(蓝线)

我发现了这篇关于这个主题的帖子,并且能够像帖子所说的那样将其导出: Can I export the result from a loess regression out of R?

但是,正如该帖子中海报的最后一条评论所说,我没有得到我的 LOESS 线的结果。有没有人对如何让它正确导出有任何见解?

谢谢!

我的导出代码在这里:

#loess object
CL111_loess <- loess(dur_cleaned~TS_LightOn, data = CL111)

#get SE
CL111_predict <- predict(CL111_loess, se=T)

CL111_ouput <- data.frame("fitted" = CL111_predict$fit, "SE"=CL111_predict$se.fit)

write.csv(CL111_ouput, "CL111_output.csv")

原始图的数据是here

我的原始情节的代码在这里:

{r}

#individual plot
ggplot(data = CL111) + geom_smooth(mapping = aes(x = TS_LightOn, y = dur_cleaned), method = "lm", se = FALSE, colour = "Green") +
labs(x = "TS Light On (Seconsd)", y = "TS Response Time (Seconds)", title = "Layout 1, Condition AO, INS High") +
  theme(plot.title = element_text(hjust = 0.5)) +
  stat_smooth(mapping = aes(x = TS_LightOn, y = dur_cleaned), method = "loess", se = TRUE) + xlim(0, 400) + ylim (0, 1.0) 

#find coefficients for best fit line

lm(CL111_LM$dur_cleaned ~ CL111_LM$TS_LightOn)
       

【问题讨论】:

    标签: r loess


    【解决方案1】:

    您可以通过ggplot_build()获取此信息。

    如果你的绘图保存为gg1,运行ggplot_build(gg1);然后您必须检查 data 对象(它是不同层的数据列表)并尝试找出您需要的层(在这种情况下,我查找了哪个数据层包含与匹配的 colour 列流畅的线条...

    bb <- ggplot_build(gg1)
    ## extract the right component, just the x/y coordinates
    out <- bb$data[[2]][,c("x","y")]
    ## check
    plot(y~x, data = out)
    

    您现在可以使用此输出做任何您想做的事情(write.csv()save()saveRDS() ...)

    我同意ggplot2 设置黄土的方式有些奇怪/我不明白。您必须使用正确的 newdata 执行 predict()(例如,具有单列 TS_LightOn 的数据框,范围从 0 到 400) - 否则您会得到数据集中点的预测,这可能不是适当间隔/以正确的顺序 - 但这并不能解决我的差异。

    【讨论】:

    • 哇,这正是我所需要的!感谢您花时间下载我的数据并真正完整地回答了这个问题......非常感谢。祝你有个愉快的夜晚!
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 2017-05-07
    • 2019-07-23
    • 2019-09-14
    • 1970-01-01
    相关资源
    最近更新 更多