【问题标题】:Multiple iterations of quadratic regression in RR中二次回归的多次迭代
【发布时间】:2015-05-01 07:10:18
【问题描述】:

我正在分析长期定期收集的气象数据(大部分数据每 15 到 60 分钟一次)。温度和太阳辐射影响的其他措施每天都有周期。如果辐射没有被云层阻挡,我试图描述一年中任何一天的平均太阳辐射暴露量。我可以访问多年的数据,并且可以根据一年中的日期平均我放入 R 中的任何数据。为了描述晴天的平均辐射量,在我做平均值之前需要丢弃一些数据。

显然我没有发布该图形的声誉,但无云日的辐射模式图具有抛物线形状。阴天可以通过具有多个峰值的曲线来识别。二次回归的R^2值可以用来区分这两种天数。

(编辑——所有辐射数据和日期/时间时间都报告在一个文本文件的两列中。我已按日期分隔下面的数据,以便任何读者轻松查看我正在尝试分析的模式,并且因为我不知道共享数据和显示模式的更复杂的方法。)

# The following vectors contain the dates and times of the readings, and the
# radiation recorded.
DateTime1<-c("13/10/23 07:00", "13/10/23 08:00", "13/10/23 09:00", "13/10/23 10:00", "13/10/23 11:00", "13/10/23 12:00", "13/10/23 13:00", "13/10/23 14:00", "13/10/23 15:00", "13/10/23 16:00", "13/10/23 17:00", "13/10/23 18:00", "13/10/23 19:00")
Sol.Rad1<-c(0, 68.78761823, 214.961307, 369.733448, 498.7102322, 576.0963027, 601.8916595, 541.7024936, 447.1195185, 352.5365434, 189.1659501, 8.598452279, 0)
DateTime2<-c("13/10/24 07:00", "13/10/24 08:00", "13/10/24 09:00", "13/10/24 10:00", "13/10/24 11:00", "13/10/24 12:00", "13/10/24 13:00", "13/10/24 14:00", "13/10/24 15:00", "13/10/24 16:00", "13/10/24 17:00", "13/10/24 18:00", "13/10/24 19:00")
Sol.Rad2<-c(0, 68.78761823, 214.961307, 369.733448, 498.7102322, 309.544282, 576.0963027, 386.9303525, 464.316423, 326.7411866, 167.6698194, 8.598452279, 0)

# The vector "Centered" is used to represent the time of day with the
# potential peak of radiation as the centered zero value.  This vector allows
# for the quadratic regressions.
Centered<-c( -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6)

# Combine the vectors into data frames; one for each day.
day1<-data.frame(DateTime1,Centered,Sol.Rad1)
day2<-data.frame(DateTime2,Centered,Sol.Rad2)

# Plotting day1 shows the parabolic shape of a cloudless day
plot(day1$Sol.Rad1 ~ day1$Centered)

# Plotting day2 shows differences in the curve (two additional peaks) due to
# cloud cover.
plot(day2$Sol.Rad2 ~ day2$Centered)

# The R^2 values from a quadratic regression of day1 are close to 0.93.
qr1<- lm(day1$Sol.Rad ~ poly(day1$Centered, 2, raw=TRUE))
summary(qr1)

# While the R^2 values from day2 are less than 0.86.
qr2<- lm(day2$Sol.Rad ~ poly(day2$Centered, 2, raw=TRUE))
summary(qr2)

如果我能找到一种方法在更大的数据集中每天重复此过程,则可以使用 R^2 的差异来区分阴天和晴天。

有没有一种方法可以从单个数据框中进行多次二次回归,其中日期和时间或所有日期的辐射读数都在单个列中报告。

理想情况下,我希望得到一个包含两列的表格。一列将包含一年中的某一天,第二列将包含来自二次回归分析的 R^2 值。我认为 Multiple R^2 或 Adjusted R^2 都可以工作(但我不太了解 R^2 的两个版本之间的区别,我无法被说服使用其中一个来代替另一个。)

我不知道如何仅报告二次回归分析中的 R^2 值,或者如何将二次回归重复次数与我分析的数据天数一样多。我可能会查看 10 年的数据,因此能够在单个表格中分析和报告分析结果将是对我可以使用哪些日期的数据进行排序的绝佳方式。

【问题讨论】:

    标签: r datetime iteration non-linear-regression


    【解决方案1】:

    我会做如下。

    首先,最好将列名标准化。

    然后使用rbind()绑定两个数据框。

    由于需要循环遍历日期,其类型已使用as.Date() 进行了修改 - 循环的两个日期如下所示。

    循环使用 lapply() 在唯一日期上完成,并创建一个数据框以同时保留 dateadjusted R squared - 当有超过 1 个解释变量时,最好依靠它。

    最后do.call() 用于绑定个别结果。

    # better to standardize column names
    day1 <- data.frame(date = DateTime1, center = Centered, sol.rad = Sol.Rad1)
    day2 <- data.frame(date = DateTime2, center = Centered, sol.rad = Sol.Rad2)
    
    # bind them in a single data frame
    days <- rbind(day1, day2)
    
    # convert into date
    days$date <- as.Date(days$date, "%y/%m/%d")
    unique(days$date)
    #[1] "2013-10-23" "2013-10-24"
    
    # lapply recursive fit lm() and put date and adjusted R sqared in a data frame
    # do.call bind them
    do.call(rbind, lapply(unique(days$date), function(x) {
      frame <- model.frame(sol.rad ~ poly(Centered, 2, raw = TRUE), data = days[days$date==x,])
      qr <- lm(sol.rad ~ ., data = frame)
      data.frame(date = x, r_sqrd = summary(qr)$adj.r.squared)
    }))
    
    #date    r_sqrd
    #1 2013-10-23 0.9232707
    #2 2013-10-24 0.8293529
    

    【讨论】:

    • Jaehyeon - 你的所有意见都很有用,决赛桌正是我想要的。
    • 对于可能使用此问题的其他任何人,我在 as.Date() 命令时遇到了问题。对于每年的数据,12 月 21 日。在使用 as.Date() 转换原始数据后,到 19-Jan 被报告为 NA。我通过创建仅包含日期(yyyy/mm/dd)而不是日期和时间的列“日期”解决了 R 之外的问题。原始日期时间数据现在位于“date.time”列中。只需 yyyy/mm/dd,R 就可以将此列作为因子读取,并且仍然可以计算出我想要的输出。
    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 2018-09-04
    • 2021-04-19
    • 2011-07-29
    相关资源
    最近更新 更多