Problem with forecast in R

Asked

Viewed 1,978 times

4

I’m trying to make a relatively simple GDP forecast, but I’m finding the following mistake:

**Error in model.frame.default(formula = y ~ t2, drop.unused.levels = TRUE) : 
  comprimentos das variáveis diferem (encontradas em 't2')**

Follow the script I’m doing:

scan=(~pib)

#Previsão

tspib = ts(pib[,2],start = c(2002,1), frequency = 12)

tspib

plot(tspib, xlab='Years', ylab = "Pib")

plot(forecast(tspib))

summary(forecast(tspib))

#Regressão

length(tspib)

t=(1:182)

t2=t^2

#Erro aqui

reg=lm(y~t+t2)

summary(reg)

The file is on my PC and is a CSV in the following format (example of the 5 first lines):

Data    PIB
2002-01 112374,80
2002-02 111477,10
2002-03 118444,70
2002-04 120385,90
2002-05 123552,50

Data is imported as "Character" GDP as "Numeric".

1 answer

5


The error is saying the problem: the variables t and t2 have different lengths of their variable response.

See the error that appears in this very simple code where the variables have different lengths.

> y = 1:9
> x1 = 1:10
> x2 = 1:10
> lm(y ~ x1 + x2)
Error in model.frame.default(formula = y ~ x1 + x2, drop.unused.levels = TRUE) : 
  O comprimentos das variáveis diferem (encontradas em 'x1')

By your code, t and t2 have for sure the same lenght, so the problem is between t and the answer.

I saw you use the lenght(tspib) and must have given 182 because you later did t <- 1:182. Check whether the tspib does not have NA information, which is automatically deleted when you adjust the regression model.

  • Daniel, I set the variable to y = GDP$GDP and it worked. I think that was the problem...!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.