2009-11-09

季節性の推定と除去

今日の講義で示したプログラムです。岡山の観測開始からの毎月の値をsheet1のA1セルから貼り付けてあるとう前提のプログラムです。

Sub 準備()
Sheets(2).Cells(1, 2) = "年"
Sheets(2).Cells(1, 3) = "月"
Sheets(2).Cells(1, 4) = "気温"
pointer = 2
'For i = 120 To 2 Step -1
For i = 2 To 120
  For j = 2 To 13
'  For j = 13 To 2 Step -1
    If i < 120 Or j < 12 Then
      Sheets(2).Cells(pointer, 2) = Sheets(1).Cells(i, 1)
      Sheets(2).Cells(pointer, 3) = Left(Sheets(1).Cells(1, j), Len(Sheets(1).Cells(1, j)) - 1)
      Sheets(2).Cells(pointer, 4) = Sheets(1).Cells(i, j)
      Sheets(2).Cells(pointer, 1) = Sheets(2).Cells(pointer, 2) & "/" & Sheets(2).Cells(pointer, 3)
      pointer = pointer + 1
    End If
  Next j
Next i
End Sub

Sub 解析()
Dim 気温差の和(12), 年数(12), 気温差(12)
For i = 1 To 12
  気温差の和(i) = 0
  年数(i) = 0
Next i
'移動平均により季節成分を除去
Sheets(2).Cells(1, 5) = "移動平均"
For i = 8 To 1421
  wa = 0
  For j = -5 To 5
    wa = wa + Sheets(2).Cells(i + j, 4)
  Next j
  wa = wa + (Sheets(2).Cells(i - 6, 4) + Sheets(2).Cells(i + 6, 4)) / 2
  Sheets(2).Cells(i, 5) = wa / 12
Next i

'季節成分を推定
For i = 8 To 1421
  気温差の和(Sheets(2).Cells(i, 3)) = 気温差の和(Sheets(2).Cells(i, 3)) + (Sheets(2).Cells(i, 4) - Sheets(2).Cells(i, 5))
  年数(Sheets(2).Cells(i, 3)) = 年数(Sheets(2).Cells(i, 3)) + 1
Next i
For i = 1 To 12
  気温差(i) = 気温差の和(i) / 年数(i)
'  Sheets(2).Cells(i, 7) = 気温差(i)
Next i
気温の差の和 = 0
For i = 1 To 12
  気温の差の和 = 気温の差の和 + 気温差(i)
Next i
Sheets(2).Cells(1, 6) = "季節調整済気温"
For i = 1 To 12
  気温差(i) = 気温差(i) - 気温の差の和 / 12
'  Sheets(2).Cells(i, 7) = 気温差(i)
Next i
For i = 2 To 1427
  Sheets(2).Cells(i, 6) = Sheets(2).Cells(i, 4) - 気温差(Sheets(2).Cells(i, 3))
Next i

End Sub

0 件のコメント: