0

I'm trying to create a linear model based off a time series analysis such as the following:

Model 1 = novice_crash ~ time + grad + time.after + month

I have the following code that creates the variables in question above:

grad<- c(replicate(66,0),replicate(30,1))
grad<- ts(grad, start=c(2002,1), frequency=12)

time<- seq(1,96, by=1)
time<- ts(time,start=c(2002,1), frequency = 12)

time.after<- c(replicate(66,0),replicate(30,1))
time.after<- ts(time.after, start=c(2002,1), frequency = 12)

#month<- seasonaldummy(novice_crashes)
month<-time

grad.lag1<- lag(grad)

time.after.lag1<- lag(time.after)

'novice_crashes' is a ts object that comes from the following code (where 'crashes' is a csv file

novice<- crash$novice_crash
total<- crash$total_crash
novice_crashes<-ts(novice, start = c(2002,12), end=c(2009,12), frequency = 12)

When I try to run this model1<- lm(novice_crashes ~ time + grad + time.after + month) I get the following error:

Error in model.frame.default(formula = novice_crashes ~ time + grad + : variable lengths differ (found for 'time')

I have checked the lengths of time, grad, time.after and month (which are all 96 units long).

The dataset crash had NA's present but I removed with

crash<- na.omit(crash)

Im much more used to python so I may be missing something here...

Josh
  • 5
  • 2
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Supply data for `crash` in the question as well. – MrFlick Oct 23 '20 at 05:57

1 Answers1

0

enter code hereI agree with the comment of MrFlick. But from what you said you did not check the lenght of the variable of novice_crashes. Since this time series starts later (start = c(2002,12)) compared to the others (start = c(2002,1)). Maybe there is the problem.

Let me know if this was the problem and otherwise post a reproducible example.

Elias
  • 726
  • 8
  • 20
  • Perfect! Thanks for looking over, you were correct in that I accidentally had put 2002,12 instead of 2002,1. I will take the comment of MrFlick for next time – Josh Oct 24 '20 at 04:12