【问题标题】:R: Variable lengths differR:可变长度不同
【发布时间】:2020-10-23 05:55:06
【问题描述】:

我正在尝试根据时间序列分析创建一个线性模型,如下所示:

模型 1 = novice_crash ~ time + grad + time.after + month

我有以下代码可以创建上述变量:

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' 是来自以下代码的 ts 对象(其中 'crashes' 是 csv 文件

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

当我尝试运行此 model1&lt;- lm(novice_crashes ~ time + grad + time.after + month) 时,我收到以下错误:

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

我检查了时间长度、毕业时间、时间.之后和月份(都是 96 个单位长)。

数据集 crash 存在 NA,但我删除了

crash<- na.omit(crash)

我更习惯于 python,所以我可能在这里遗漏了一些东西......

【问题讨论】:

  • 如果您包含一个简单的reproducible example 以及可用于测试和验证可能的解决方案的示例输入,则更容易为您提供帮助。为问题中的crash 提供数据。

标签: r time-series lm


【解决方案1】:

enter code here我同意 MrFlick 的评论。但是从你所说的你没有检查novice_crashes 变量的长度。由于与其他时间序列 (start = c(2002,1)) 相比,此时间序列开始较晚 (start = c(2002,12))。可能有问题。

让我知道这是否是问题所在,否则发布可重现的示例。

【讨论】:

  • 完美!感谢您查看,您是正确的,因为我不小心放了 2002,12 而不是 2002,1。下次我会接受 MrFlick 的评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
相关资源
最近更新 更多