【近似曲線】Excelを使って計算式を元にデータを予測する方法!

こんにちは!『ルウちゃんのExcel Labo』管理人の@rouxchanblogです!
今回はこのような悩みについて解説します。

過去のデータから予測する方法を教えてほしい!
- グラフから近似曲線を出す方法
- 近似曲線から数式を活用する方法
- 計算式からデータを予想する方法
はじめに
今回はExcelの機能を使って、過去のデータから未来の数値を予想する方法を解説します。
例として、ブログの経過月数とPV数(表示回数)を使って予想をしていきましょう!
グラフから近似曲線を出す方法
まずは近似曲線というグラフを表示していきます。
このようにグラフの元データとなる表を作成してください。
今回はPV数と月数を表示していきます。


次に通し番号を追加します。
今回は月数という列を追加して1〜12までの数字を振りました。


データを選択してグラフの種類を選択します。
データ範囲は「B1:C8」としました。
今回は折れ線グラフを選択しました。








近似曲線から数式を取り出す方法
次は追加した近似曲線からこのグラフの数式を取り出します。
近似曲線を右クリックしてオプションを開きます。
「多項式近似」にチェックを入れ、次数を2にします。
次数が2までになるような多項式が自動で生成されます。
次に「グラフに数式を表示する」にチェックを入れます。
ここにチェックを入れることでグラフ上に数式を表示させることができます。
最後に「グラフにR-2乗値を表示する」にチェックを入れます。
R-2乗値を表示させることにより、この近似曲線の信憑性を判断することができます。
R^2が1に近いほど正しい結果を得られやすいです。




表示させた数式の「y=〇〇」が使用する数式となります。ここをコピーしましょう。


数式からデータ予測をする方法
ここからは表示した数式を実際に使ってデータ予測をしていきます。




数式に予測したい月の経過月数を代入すると、予測数を出すことができます。
今回は11月の数値を予測したので、1660PVと表示されました


1つ予測結果がでれば、あとはそのセルをコピーすれば次月以降の予測も簡単に出すことができます。


近似曲線のオプションから「予測」の「前方補外」に表示したい区間を入力すると、グラフに予測を反映させることができます。




マクロで数式に代入する方法
最後にこの「代入して連続データのコピー」をマクロで実行してみましょう。
まずはこのコードでグラフの数式を取得します。
こちらのコードはヒガサラBlog.様のものを使わせていただきました。
Sub 近似曲線の数式を取得()
'変数の型を宣言
Dim x_ax As String, y_ax As String
Dim grf_formula As String
Dim a As Single, b As Single, c As Single
'データ範囲を指定する(やるのはここだけ)
x_ax = "A2:A11"
y_ax = "B2:B11"
'グラフ(散布図)を挿入する
ActiveSheet.Shapes.AddChart2(-1, -4169).Select
With ActiveChart
'指定したデータでグラフ作成
.Parent.Name = "sample_grf"
.SeriesCollection.NewSeries
With .FullSeriesCollection(1)
.XValues = ActiveSheet.Name & "!" & x_ax
.Values = ActiveSheet.Name & "!" & y_ax
End With
End With
'グラフに近似曲線を追加する
With ActiveChart.SeriesCollection(1)
.Trendlines.Add
.Trendlines(1).Select
End With
'近似曲線の種類を指定する
With Selection
.Type = xlPolynomial
.Order = 2
End With
Selection.DisplayEquation = True
'近似曲線の式の桁数を指定する
ActiveSheet.ChartObjects("sample_grf").Activate
With ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel
.NumberFormat = "#,##0.000000_ "
End With
'グラフの近似式をリフレッシュ
ActiveSheet.ChartObjects("sample_grf").Activate
ActiveChart.Refresh
'グラフの近似式を取得する
With ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel
grf_formula = .Text
End With
ActiveSheet.ChartObjects("sample_grf").Delete
split_formula = Split(grf_formula, " ")
a = split_formula(2)
b = split_formula(4) & split_formula(5)
c = split_formula(7) & split_formula(8)
'数式&係数をセルに書き出す
Cells(1, 4) = grf_formula
Cells(2, 4) = "a="
Cells(2, 5) = a
Cells(3, 4) = "b="
Cells(3, 5) = b
Cells(4, 4) = "c="
Cells(4, 5) = c
End Sub


グラフの数式を取得することができました。
次に取得した数式の中の特定の文字を置き換えてセルに転記していきます。
- “y” →””
- “x2” →”*A12^2″
- “x” →”*A12″
- ” ” →”” 空白を消すため
あとは「autofillメソッド」で連続コピーすれば完成です。
Sub 文字を置き換えて連続コピー()
Dim rng As String
Dim i As Long
Dim top As Long
Dim bottom As Long
top = Cells(Rows.Count, 2).End(xlUp).Row
bottom = Cells(Rows.Count, 1).End(xlUp).Row
rng = Cells(1, 4)
rng = Replace(rng, "x2", "*A12^2") 'A12の部分は指定する
rng = Replace(rng, "x", "*A12") 'A12の部分は指定する
rng = Replace(rng, " ", "")
rng = Replace(rng, "y", "")
Cells(top + 1, 2) = rng
Cells(top + 1, 2).AutoFill Destination:=Range(Cells(top + 1, 2), Cells(bottom, 2))
End Sub


これで代入された式がセルに入り、連続コピーもすることができました。
文字の置き換えでは、文字を変数で入れることができずコードを逐一書き換える必要があります。
これではまだまだ手作業と変わらないので、取得した係数に変数を組み合わせて数式を記述できるようにしたいですね。
自分が納得できるものができたらまたあげるかもしれないです。
まとめ
- グラフを作り、近似曲線を表示させる
- 近似曲線から数式を取り出す
- 数式にセルの値を代入して、予測結果を表示する
今回はExcelの機能を使って、過去のデータから未来の数値を予想する方法を解説しました。
一見小難しそうに見えますが、慣れれば手軽に結果予測をすることができ、便利です。
実際の結果に近い値を出すにはたくさんのデータを集める必要はありますが、Excelさえあれば手軽にできるのでおすすめです!
この記事で少しでも皆さんのお役に立てれば幸いです!
忘れないうちにお気に入り登録もお願いします!
ありがとうございました!
コメントはお気軽に!
コメント一覧 (3件)
とても参考になりました。
ありがとうございます。
追加で知りたいのですが、公式のコピペをマクロで実行することは可能でしょうか?
コメントありがとうございます!
お役に立てて嬉しいです!
数式に代入してコピペする方法を記事に追記しました。
まだまだ改善の余地はありますが一度ご覧ください(^_^)
返信おそくなりすみません。
ご回答、ご対応ありがとうございました。
早速定義利用させていただきました。
おかげさまで、やろうとしていることに一歩近づきました。
また行き詰まったらアドバイスお願い致します。