繁体   English   中英

R中二次回归的多次迭代

[英]Multiple iterations of quadratic regression in R

我正在分析定期定期收集的长期气象数据(大多数数据每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值。 我认为倍数R ^ 2或调整后的R ^ 2都可以工作(但是我对R ^ 2的两个版本之间的差异了解不多,我无法说服我使用一个版本来代替另一个。)

我不知道如何仅从二次回归分析中报告R ^ 2值 ,或者如何重复二次回归与我正在分析的数据天一样多的次数 我可能正在查看10年的数据,因此能够在单个表中分析和报告分析的结果将是一种很好的方式来分类我可以使用的数据天数。

我会做如下。

首先,最好对列名称进行标准化。

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

由于有必要循环日期,因此已使用as.Date()修改了其类型-要循环显示的两个日期如下所示。

使用lapply()在唯一的日期上进行循环,并创建一个数据框以同时保留dateadjusted R squared -最好在有多个解释变量时才依赖此。

最后, 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM