2021年2月9日星期二

How to train a linear model iteratively depending on days and date range

I would like to fit/train a predictive model and do it intertatively, that is, for example, I train my model every 50 days in the selected period (here: all of 2020). Basically I want to predict the second column (DE) and use the remaining columns and parameters for the prediction. My data table can look like this:

set.seed(123)  days <- 50  ## Create random data table: ##  dt.data <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 366),                        "DE" = rnorm(366, 35, 1), "Wind" = rnorm(366, 5000, 2), "Solar" = rnorm(366, 3, 2),                        "Nuclear" = rnorm(366, 100, 5), "ResLoad" = rnorm(366, 200, 3),  check.names = FALSE)  

The date range of my data table and the number of days (days) can always differ. I have already done the model fitting/training for the whole data table once, but I do not know how to do this iteratively every 50 days? Here you can see a code snippet of my model fitting for a linear model:

v.trainDate <- dt.data$date  ## Delete column "date" of train data for model fitting: ##  dt.data <- dt.data[, c("date") := NULL]    ## MODEL FITTING: ##  ## Linear Model: ##  lmModel <- stats::lm(DE ~ .-1, data = dt.data)    ## Train PREDICTION with lmModel: ##  dt.data$prediction <- stats::predict.glm(lmModel, dt.data)  ## Add date columns to dt.train: ##  dt.data <- data.table(date = v.trainDate, dt.data)  

What I want to have at the end is that I train the model with my data first from 2020-01-01 to 2020-02-20 (first 50 days) and predict the DE price with this fitted model lmModel for the first fifty entries of my data table. Next run should be to train my model from 2020-02-20 to 2020-04-10 (next 50 days) and predict the values for this new 50 days. This should be done until the last December day fo 2020. At the end I need a column, calledprediction as you can see in my code snippet, but this column should consist the interatively constructed predictions of the DE price.

I would also like to save the variable Importance somewhere after each iteration? So that I can see which variable had the most influence on the DE price in the first 50 days, etc. Does anyone know how this could work?

https://stackoverflow.com/questions/66119857/how-to-train-a-linear-model-iteratively-depending-on-days-and-date-range February 09, 2021 at 09:21PM

没有评论:

发表评论