PeterBe
PeterBe

Reputation: 842

Error message about the plot in regression model

I have an R programm for a regression that somehow gives me an error message that I do not understand. The regression model takes as input heat input heat data (Q_htg) and the corresponding temperature data (T_amb) and then builds a linear regression for those two variables. Afterwards I want to use the trained regression model to predict some outputs. Here is the code:

dalinearPowerScaling2.function <-
  function(Dataset,
           numberOfDaysForAggregation,
           normOutsideTemperature) {
    heatingPower <- Dataset$Q_htg
    outSideTemperature <- Dataset$T_amb
    aggregationLevel <- numberOfDaysForAggregation * 1440
    index <- 0
    meanValuesOutsideTemperature <-
      vector(, length(outSideTemperature) / aggregationLevel)
    for (i in seq(1, length(outSideTemperature), aggregationLevel)) {
      sum <- 0
      for (j in seq(i, i + aggregationLevel - 1, 1)) {
        sum <- sum + outSideTemperature[j]
      }
      index <- index + 1
      meanValuesOutsideTemperature[index] <- sum / aggregationLevel
    }
    index <- 0
    meanValuesHeatingDemand <-
      vector(, length(heatingPower) / aggregationLevel)
    for (i in seq(1, length(heatingPower), aggregationLevel)) {
      sum <- 0
      for (j in seq(i, i + aggregationLevel - 1, 1)) {
        sum <- sum + heatingPower[j]
      }
      index <- index + 1
      meanValuesHeatingDemand[index] <- sum / aggregationLevel
    }

    linearModel <-
      lm(meanValuesHeatingDemand ~ meanValuesOutsideTemperature)
    abline(linearModel, col = "red")

    pred <- predict(linearModel, data.frame(meanValuesOutsideTemperature = c(normOutsideTemperature)))
    List<-list(meanValuesHeatingDemand, meanValuesOutsideTemperature)
    List2 <- vector("list", length(heatingPower)/aggregationLevel)
    for (i in seq(1, length(meanValuesHeatingDemand),1)){
    List2 [[i]]<-c(meanValuesHeatingDemand[i], meanValuesOutsideTemperature[i])
      }

    List3<-List2[order(sapply(List2, function(x) x[1], simplify=TRUE), decreasing=FALSE)]

    firstTemperatureWithHeatingDemand<-0
    firstHeatingDemand<-0
    for (i in seq(1, length(List3), 1)) {
    if(List3[[i]][1]>0) {
      firstTemperatureWithHeatingDemand<-List3[[i]][2]
      firstHeatingDemand<-List3[[i]][1]
      break}
    }
    regression2ValuesX <- vector(, 5)
    regression2ValuesY <- vector(, 5)
    regression2ValuesX [1] <- firstTemperatureWithHeatingDemand
    regression2ValuesY [1] <-firstHeatingDemand
    List3<-List2[order(sapply(List2, function(x) x[1], simplify=TRUE), decreasing=TRUE)]
    for (i in seq(1, length(regression2ValuesX) - 1, 1)) {
      regression2ValuesX[i + 1]<-List3[[i]][2]
      regression2ValuesY[i + 1]<-List3[[i]][1]
      }

    plot(regression2ValuesX, regression2ValuesY)
    linearModel2 <-
      lm(regression2ValuesY ~ regression2ValuesX)
    abline(linearModel2, col = "blue")
    pred <- predict(linearModel2, data.frame(regression2ValuesX = c(normOutsideTemperature)))
    paste("Predicted heating demand:", round(pred))
  }

When I run with the command

linearPowerScaling2.function(data_heat_test, 1, -12)

I get the error message:

 Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) : 
  plot.new has not been called yet 
3.
int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) 
2.
abline(linearModel, col = "red") at LinearPowerScaling2_Function.R#33
1.
linearPowerScaling2.function(data_heat_test, 1, -12) 

The data itself should be okay. Can anyone tell me, what the problem is?

Upvotes: 0

Views: 63

Answers (1)

dario
dario

Reputation: 6483

Without reproducible minimal example it's hard to test if this solves it, but the error message tells you that you are calling abline() before calling plot().

That's exactly what happens on line 33...

Hope this helps.

Check here to see how to make a minimal reproducible example.

Upvotes: 1

Related Questions