多元线性回归模型以及 R 中的整体 ggplot?
Multiple linear regression model along with ensemble ggplot in R?
我正在尝试 predict
6 月 - 9 月 Level
Year 2020
使用 multiple linear regression model
。在我下面的示例中,我假设 2016 年的条件将重复并将其用于预测 2020 年 6 月至 9 月的水平。我 plot
观察到的水平直到 May 31
,显示为黑色实线和Forecasted Level
显示为蓝色虚线。
library(tidyverse)
library(lubridate)
set.seed(1500)
DF <- data.frame(Date = seq(as.Date("2000-01-01"), to = as.Date("2018-12-31"), by = "days"),
Level = runif(6940, 360, 366), Flow = runif(6940, 1,10),
PCP = runif(6940, 0,25), MeanT = runif(6940, 1, 30)) %>%
mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>%
filter(between(Month, 6, 9))
Model <- lm(data = DF, Level~Flow+PCP+MeanT)
Yr_2016 <- DF %>%
filter(Year == 2016) %>%
select(c(3:5))
Pred2020 <- data.frame(Date = seq(as.Date("2020-06-01"), to = as.Date("2020-9-30"), by = "days"),
Forecast = predict(Model, Yr_2016))
Obs2020 <- data.frame(Date = seq(as.Date("2020-01-01"), to = as.Date("2020-05-31"), by = "days"),
Level = runif(152, 360, 366))
ggplot(data = Obs2020, aes(x = Date, y = Level), col = "black")+
geom_line(size = 2)+
geom_line(data = Pred2020, aes(x = Date, y = Forecast), linetype = "dashed")
我的目标
我想使用 fitted model
到 predic
t 6 月 - 9 月 2020
假设 DF
中的所有年份都会重复(不仅仅是2016 年)然后有一个 plot
,其中所有年份 Forecasted
场景(6 月至 9 月)都以不同的颜色显示 - 如下所示
新答案
下面的代码应该可以满足您的需求(如果我理解正确的话)。然而,该图仍然是混乱的。
library(tidyverse)
library(lubridate)
set.seed(1500)
DF <- data.frame(Date = seq(as.Date("2000-01-01"), to = as.Date("2018-12-31"), by = "days"),
Level = runif(6940, 360, 366), Flow = runif(6940, 1,10),
PCP = runif(6940, 0,25), MeanT = runif(6940, 1, 30)) %>%
mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>%
filter(between(Month, 6, 9))
Model <- lm(data = DF, Level ~ Flow + PCP + MeanT)
Obs2020 <- data.frame(Date = seq(as.Date("2020-01-01"),
to = as.Date("2020-05-31"),
by = "days"),
Level = runif(152, 362.7, 363.25))
pred_data <- DF %>%
nest_by(Year) %>%
mutate(pred_df = list(tibble(Date = seq(as.Date("2020-06-01"),
to = as.Date("2020-09-30"),
by = "days"),
Forecast = predict(.env$Model, data)))) %>%
select(Year, pred_df) %>%
unnest(pred_df)
ggplot(data = Obs2020, aes(x = Date, y = Level), col = "black") +
geom_line(size = 0.1) +
geom_line(data = pred_data,
aes(x = Date, y = Forecast, group = factor(Year), color = factor(Year)),
size = 0.1)
由 reprex package (v0.3.0)
于 2020 年 6 月 20 日创建
我正在尝试 predict
6 月 - 9 月 Level
Year 2020
使用 multiple linear regression model
。在我下面的示例中,我假设 2016 年的条件将重复并将其用于预测 2020 年 6 月至 9 月的水平。我 plot
观察到的水平直到 May 31
,显示为黑色实线和Forecasted Level
显示为蓝色虚线。
library(tidyverse)
library(lubridate)
set.seed(1500)
DF <- data.frame(Date = seq(as.Date("2000-01-01"), to = as.Date("2018-12-31"), by = "days"),
Level = runif(6940, 360, 366), Flow = runif(6940, 1,10),
PCP = runif(6940, 0,25), MeanT = runif(6940, 1, 30)) %>%
mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>%
filter(between(Month, 6, 9))
Model <- lm(data = DF, Level~Flow+PCP+MeanT)
Yr_2016 <- DF %>%
filter(Year == 2016) %>%
select(c(3:5))
Pred2020 <- data.frame(Date = seq(as.Date("2020-06-01"), to = as.Date("2020-9-30"), by = "days"),
Forecast = predict(Model, Yr_2016))
Obs2020 <- data.frame(Date = seq(as.Date("2020-01-01"), to = as.Date("2020-05-31"), by = "days"),
Level = runif(152, 360, 366))
ggplot(data = Obs2020, aes(x = Date, y = Level), col = "black")+
geom_line(size = 2)+
geom_line(data = Pred2020, aes(x = Date, y = Forecast), linetype = "dashed")
我的目标
我想使用 fitted model
到 predic
t 6 月 - 9 月 2020
假设 DF
中的所有年份都会重复(不仅仅是2016 年)然后有一个 plot
,其中所有年份 Forecasted
场景(6 月至 9 月)都以不同的颜色显示 - 如下所示
新答案
下面的代码应该可以满足您的需求(如果我理解正确的话)。然而,该图仍然是混乱的。
library(tidyverse)
library(lubridate)
set.seed(1500)
DF <- data.frame(Date = seq(as.Date("2000-01-01"), to = as.Date("2018-12-31"), by = "days"),
Level = runif(6940, 360, 366), Flow = runif(6940, 1,10),
PCP = runif(6940, 0,25), MeanT = runif(6940, 1, 30)) %>%
mutate(Year = year(Date), Month = month(Date), Day = day(Date)) %>%
filter(between(Month, 6, 9))
Model <- lm(data = DF, Level ~ Flow + PCP + MeanT)
Obs2020 <- data.frame(Date = seq(as.Date("2020-01-01"),
to = as.Date("2020-05-31"),
by = "days"),
Level = runif(152, 362.7, 363.25))
pred_data <- DF %>%
nest_by(Year) %>%
mutate(pred_df = list(tibble(Date = seq(as.Date("2020-06-01"),
to = as.Date("2020-09-30"),
by = "days"),
Forecast = predict(.env$Model, data)))) %>%
select(Year, pred_df) %>%
unnest(pred_df)
ggplot(data = Obs2020, aes(x = Date, y = Level), col = "black") +
geom_line(size = 0.1) +
geom_line(data = pred_data,
aes(x = Date, y = Forecast, group = factor(Year), color = factor(Year)),
size = 0.1)
由 reprex package (v0.3.0)
于 2020 年 6 月 20 日创建