Linear model for each month(jan-dec)

I have a dataset daf that ranges from 1976-1982. I would like to find the monthly regression model of its variance. I tried with the following code for its regression model. could anyone suggest to me how can I get for 12 months i.e. Ja... Dec respectively.


  datetime            measured   synthetic yrmonth
  <dttm>                 <dbl>     <dbl> <chr>  
1 1976-09-03 22:45:00     0            0 1976-09
2 1976-09-03 23:00:00     2.54         0 1976-09
3 1976-09-03 23:15:00     5.08         0 1976-09
4 1976-09-03 23:30:00     0            0 1976-09
5 1976-09-03 23:45:00     0            6 1976-09
6 1976-09-04 00:00:00     0            1 1976-09

library(tidyverse)
daf=read_csv("df.csv",col_names=TRUE)
daf$datetime=as.POSIXct(daf$datetime, format="%Y-%m-%d %H:%M:%S")
daf$yrmonth=format(as.Date(daf$datetime, format="%Y-%m-%d %H:%M:%S"),"%Y-%m")

df_var <- aggregate(cbind(measured,synthetic)    ~ yrmonth, daf,  var)

model <- lm(synthetic   ~ measured, data = df_var)

Generally (due to the lack of a reprex) lm on time series data are vulnerable to autocorrelation in the residuals. See fpp2.

To subset the data by year, dplyr::filter(lubridate::year(the_date) == the_year. If doing this for a number of years, use purrr::map

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.