You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从geom_smooth(loess)提取整数天数对应的预测温度值?

解决LOESS拟合后获取整数天数对应温度值的问题

不用从ggplot_build提取拟合结果,直接用R的loess和predict函数就能精准获取每个整数天数的预测温度,步骤如下:

  1. 拟合LOESS模型
    直接基于原始数据构建loess拟合模型,这是最可靠的方式:

    # 基于现有数据拟合loess模型
    loess_fit <- loess(Temperature4.5 ~ DayNumber, data = med4.5)
    
  2. 生成目标整数天数序列
    生成你需要的所有整数天数范围(45到410):

    # 生成45到410的所有整数天数
    target_days <- seq(45, 410, by = 1)
    
  3. 预测每个整数天的温度
    用拟合好的模型对所有整数天数做预测,结果会自动对应每个整数x:

    # 预测并整理成数据框
    daily_temp_pred <- data.frame(
      DayNumber = target_days,
      Predicted_Temperature = predict(loess_fit, newdata = data.frame(DayNumber = target_days))
    )
    

补充说明

  • 原代码中med4.5的Season和Date变量未给出定义,如果需要将预测结果与这两个变量关联,可以通过DayNumber字段匹配合并数据。
  • 这种方法比从ggplot_build提取后插值更准确,因为直接使用拟合模型的预测逻辑,避免了中间采样的误差。

内容的提问来源于stack exchange,提问作者EcoQuestions

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 18:36:23