Problem with forecast in R

4

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

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

Here's 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 in my PC and is a CSV in the following format (example of the first 5 lines):

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

Date is imported as "character" PIB as "numeric".

    
asked by anonymous 26.04.2017 / 20:35

1 answer

3

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

See the error that appears in this very simple code in which 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')

For your code, t and t2 are sure to have lenght , so the problem is between t and response.

I saw that you use lenght(tspib) and should have given 182 because you then made t <- 1:182 . Verify that tspib has no NA information, which is automatically excluded when you adjust the regression model.

    
26.04.2017 / 23:38