氣溫是漂移式隨機漫步嗎?
ARMA 模型用來預測氣候變化相對簡化,但能展現本課多項重點。
temp_NY 這個 DataFrame 包含紐約中央公園從 1870–2016 年的年度平均氣溫(資料來自 NOAA,下載連結在此:here)。請把資料畫出來,並檢驗它是否遵循漂移式隨機漫步。
本練習屬於課程
Python 中的時間序列分析
練習說明
- 使用
pd.to_datetime()將年份索引轉成 datetime 物件;因為是年度資料,請傳入參數format='%Y'。 - 使用
.plot()繪製資料。 - 使用
adfuller函式計算擴充 Dickey–Fuller(ADF)檢定的 p 值。 - 將 ADF 檢定結果存入
result,並列印result[1]中的 p 值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the adfuller function from the statsmodels module
from statsmodels.tsa.stattools import adfuller
# Convert the index to a datetime object
temp_NY.index = pd.to_datetime(___.___, format=___)
# Plot average temperatures
temp_NY.___
plt.show()
# Compute and print ADF p-value
result = ___(temp_NY['TAVG'])
print("The p-value for the ADF test is ", result[1])