[英]R: Variable lengths differ
我正在嘗試基於時間序列分析創建一個線性模型,如下所示:
模型 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<- 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,所以我可能在這里遺漏了一些東西......
enter code here
我同意 MrFlick 的評論。 但是根據您所說的,您沒有檢查novice_crashes
變量的novice_crashes
。 由於與其他時間序列( start = c(2002,1)
)相比,這個時間序列開始start = c(2002,12)
晚( start = c(2002,12)
start = c(2002,1)
)。 也許有問題。
讓我知道這是否是問題所在,否則發布一個可重現的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.