# price prediction in r using time series

I am trying to make a prediction of imbalance prices in the elctricity market. My dataset consists of data for every 15 minutes (this is the time period in which a price is determined) during 11 months. I have several exogenous factors (like the spot market price) included mentioned here as x1 etc.

In forecasting the price I am using the following code:

lag <- function(x, k){c(rep(NA, k), x)[1 : length(x)]}
mydata$y_lag1 <- lag(mydata$y, 1)
mydata$y_lag2 <- lag(mydata$y, 2)
mydata$x1_lag1 <- lag(mydata$x1, 1)
mydata$x2_lag1 <- lag(mydata$x2, 1)
mydata$x3_lag1 <- lag(mydata$x3, 1

f<- y ~ y_lag1 + y_lag2 + x1_lag1 + x2_lag1 + x3_lag1
fit <- lm(formula = f, data = mydata)
mydata$P_imb_pred <- predict(fit, newdata = mydata) pred <- data.frame(time=mydata$time, price=mydata\$P_imb_pred)


My code works, but I am unsure if it does wat I want it to. I am trying to predict the price only 1 time unit (so 15 minutes) ahead. That's why I have lagged variables in the function. Can someone help me out? Should I additionally specify how much time ahead I want to forecast? If so, can you tell me how?