~~~
[0]前提
各業種でその日の新聞から株価を転記して(たとえば、電機では約160銘柄)、
べき乗傾向の線形近似をExcelで求める作業は、約30分程度要していた。
Stock prices were posted from the newspaper of the day by each type of
business (For instance, they were about 160 brands in the Electric Appliances),
and work to request a linear approximation of the Power Law tendency
by Excel was required for about 30 minutes.
今後作業を継続する場合でも、今回の時価総額による作業をやり直す場合でも、
省力化のために、Rでの対応方法を確認する。
Even when work by this aggregate market value does over again even when work will
be continued in the future, the correspondence method in R is confirmed for the labor saving.
Excelの作業と同様に、Rで、相関図のグラフや近似式を得る方法を確認する。
The method of obtaining the graph and the approximation type of the correlation
diagram is confirmed by R as well as the work of Excel.
~~~
[1]探索
以下のように検索して行き着いた先は、、、
:探索履歴をメモりながら、進んでいきます。いつものパターン。
:キーワードの組み合わせを再考する場合には、記録してあると便利です。for myself.
---
google:R 最小二乗
google:R 線形近似
google:線形近似 プログラム
google:線形近似 最小二乗
===
http://szksrv.isc.chubu.ac.jp/lms/lms7.html
Excelをつかった最小二乗法
:先に進みます。
===
google:線形近似 最小二乗 R
google:Excel 線形近似 R プログラム
===
http://ssl.ohmsha.co.jp/cgi-bin/menu.cgi?ISBN=4-274-06556-1
Excelで学ぶ回帰分析入門、オーム社
--
第2章 散布図でデータの関係を調べよう
2.1 散布図とは
2.2 散布図のxとyの関係
2.3 単回帰式(関係式)を求めよう
2.3.1 直線(線形近似)の挿入
2.3.2 単回帰式とは
2.3.3 相関があるかどうかを調べる
2.3.4 最小自乗法
--
:線形は、単回帰でした。キーワード変更。
===
google:R 単回帰
===
http://cse.naro.affrc.go.jp/takezawa/r-tips/r/70.html
単回帰分析
:たどり着きました。
~~~
[2]動作確認
[2-1]サンプルデータ
20081212付、東証一部、食品、終値の時価総額
:69銘柄、時価総額順に降順。
:owarine=終値, stock_num=発行済み株式数(単位:千株),jika=時価総額
:LN_order=LN(順番), LN_jika=LN(終値の時価総額)
file:20081212-foods.txt
===
code,date,name,owarine,stock_num,jika,order,LN_order,LN_jika
2914,20081212,JT,311000,10000,3110000000,1,0,21.85788856
2503,20081212,キリン,1082,984508,1065237656,2,0.693147181,20.78646376
2502,20081212,アサヒ,1546,483586,747623956,3,1.098612289,20.43241068
2802,20081212,味の素,942,700033,659431086,4,1.386294361,20.30688803
2897,20081212,日清食品,3290,127464,419356560,5,1.609437912,19.85423209
2267,20081212,ヤクルト,1752,175910,308194320,6,1.791759469,19.54624105
2875,20081212,東洋水産,2645,110881,293280245,7,1.945910149,19.49663918
2212,20081212,山崎製パン,1284,220283,282843372,8,2.079441542,19.46040385
2282,20081212,日本ハム,1202,228445,274590890,9,2.197224577,19.43079288
2002,20081212,日清製粉,1078,251535,271154730,10,2.302585093,19.41820018
2579,20081212,北九州コカ,1942,111126,215806692,11,2.397895273,19.18989362
2801,20081212,キッコマン,978,210383,205754574,12,2.48490665,19.14219463
2501,20081212,サッポロ,507,393971,199743297,13,2.564949357,19.11254359
2809,20081212,キユーピー,1188,155465,184692420,14,2.63905733,19.0342024
2810,20081212,ハウス食品,1586,110879,175854094,15,2.708050201,18.9851652
2202,20081212,明治製菓,408,385535,157298280,16,2.772588722,18.87365443
2261,20081212,明治乳業,466,329649,153616434,17,2.833213344,18.84996937
2811,20081212,カゴメ,1493,99617,148728181,18,2.890371758,18.81763091
2206,20081212,江崎グリコ,1010,144860,146308600,19,2.944438979,18.80122865
2871,20081212,ニチレイ,394,310851,122475294,20,2.995732274,18.62341989
2593,20081212,伊藤園,1260,91212,114927120,21,3.044522438,18.55980875
2607,20081212,不二製油,1242,87569,108760698,22,3.091042453,18.5046606
2531,20081212,宝ホールディ,488,217700,106237600,23,3.135494216,18.48118865
2262,20081212,雪印乳業,335,303802,101773670,24,3.17805383,18.43826198
2059,20081212,ユニチャーム,3250,29360,95420000,25,3.218875825,18.37379876
2264,20081212,森永乳業,345,253977,87622065,26,3.258096538,18.28854341
2602,20081212,日清オイリオ,471,173339,81642669,27,3.295836866,18.21786259
2001,20081212,日本製粉,461,174148,80282228,28,3.33220451,18.20105883
2284,20081212,伊藤ハム,315,210483,66302145,29,3.36729583,18.00973281
2580,20081212,コカコーラセ,654000,90,58860000,30,3.401197382,17.8906723
2613,20081212,J-オイルミ,335,167542,56126570,31,3.433987204,17.84311988
2004,20081212,昭和産業,296,180650,53472400,32,3.465735903,17.79467619
2201,20081212,森永製菓,189,270949,51209361,33,3.496507561,17.75143291
2109,20081212,三井製糖,354,141667,50150118,34,3.526360525,17.73053143
2572,20081212,三国コカ,853,53556,45683268,35,3.555348061,17.63724266
2815,20081212,アリアケ,1359,32809,44587431,36,3.583518938,17.61296256
2207,20081212,名糖産業,1884,21265,40063260,37,3.610917913,17.50597026
2590,20081212,ダイドードリ,2375,16569,39351375,38,3.63758616,17.48804148
2908,20081212,フジッコ,1120,34992,39191040,39,3.663561646,17.48395871
2281,20081212,プリマハム,171,224393,38371203,40,3.688879454,17.46281781
2108,20081212,日本甜菜糖,244,153256,37394464,41,3.713572067,17.43703323
2288,20081212,丸大食品,268,132528,35517504,42,3.737669618,17.3855362
2594,20081212,キーコーヒ,1554,22464,34909056,43,3.761200116,17.36825684
2899,20081212,永谷園,857,38277,32803389,44,3.784189634,17.30604239
2290,20081212,米久,1134,28810,32670540,45,3.80666249,17.30198431
2204,20081212,中村屋,503,59762,30060286,46,3.828641396,17.21871546
2540,20081212,養命酒製造,840,33000,27720000,47,3.850147602,17.13766473
2918,20081212,わらべや日洋,1561,16626,25953186,48,3.871201011,17.07180493
2536,20081212,メルシャン,183,133689,24465087,49,3.891820298,17.01275764
2211,20081212,不二家,119,194377,23130863,50,3.912023005,16.95667834
2292,20081212,SFOODS,711,32268,22942548,51,3.931825633,16.94850374
2051,20081212,日本農産工,172,129310,22241320,52,3.951243719,16.91746238
2578,20081212,四国コカ,860,23908,20560880,53,3.970291914,16.8389008
2009,20081212,鳥越製粉,760,26036,19787360,54,3.988984047,16.80055391
2053,20081212,中部飼料,599,26536,15895064,55,4.007333185,16.58151918
2910,20081212,ロックフィ,1166,13394,15617404,56,4.025351691,16.56389649
2533,20081212,オエノンホー,218,65586,14297748,57,4.043051268,16.4756126
2812,20081212,焼津水産化学,1000,14056,14056000,58,4.060443011,16.45855991
2003,20081212,日東製粉,287,46924,13467188,59,4.077537444,16.41576677
2922,20081212,なとり,709,15532,11012188,60,4.094344562,16.21451322
2217,20081212,モロゾフ,300,36692,11007600,61,4.110873864,16.2140965
2052,20081212,協同飼料,101,103996,10503596,62,4.127134385,16.16722823
4404,20081212,ミヨシ油脂,125,82455,10306875,63,4.143134726,16.14832171
2597,20081212,ユニカフェ,1118,6869,7679542,64,4.158883083,15.85407047
2286,20081212,林兼産業,71,89100,6326100,65,4.17438727,15.66019449
2056,20081212,日配合飼料,86,71877,6181422,66,4.189654742,15.6370589
2107,20081212,東洋精糖,95,54560,5183200,67,4.204692619,15.46093318
2215,20081212,第一パン,93,48048,4468464,68,4.219507705,15.31255528
2599,20081212,ジャパンフー,691,5100,3524100,69,4.234106505,15.07513564
===
---
[2-2]グラフ出力+線形近似
foods = read.csv("20081212-foods.txt");
plot(LN_jika ~ LN_order, data = foods)
# 線形近似を求める。
result = lm(LN_jika ~ LN_order, data = foods)
# 近似曲線をグラフに追加。
abline(result)
# 近似結果を出力
summary(result)
===
> summary(result)
Call:
lm(formula = LN_jika ~ LN_order, data = foods)
Residuals:
Min 1Q Median 3Q Max
-1.3499 -0.2369 0.1748 0.2605 0.5010
Coefficients:
Estimate Std. Error t value Pr(>t)
(Intercept) 22.58154 0.19806 114.02 <2e-16 ***
LN_order -1.45403 0.05825 -24.96 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4366 on 67 degrees of freedom
Multiple R-squared: 0.9029, Adjusted R-squared: 0.9015
F-statistic: 623.1 on 1 and 67 DF, p-value: < 2.2e-16
>
> result
Call:
lm(formula = LN_jika ~ LN_order, data = foods)
Coefficients:
(Intercept) LN_order
22.582 -1.454
>
===
:これから、線形近似式は以下となる。
LN(y) = -1.45403*LN(x) + 22.58154 , R^2=0.9029
---
Excelで同様に求めた場合は以下。
LN(y) = -1.454*LN(x) + 22.582 , R^2=0.9029

---
[2-3]R の summary から結果を抜き出す。
:抜出の情報があまりない、、、
:結果を抜出、処理を自動化するのが目的です。
===
参考ページは以下。
http://cse.naro.affrc.go.jp/takezawa/r-tips/r/71.html
:メモ整理中。names()もあったのを気づくが、この段階では使用していない。
===
試行錯誤で行き着いたのが、、、
> result = summary(result)
> result
Length Class Mode
call 3 -none- call
terms 3 terms call
residuals 69 -none- numeric
coefficients 8 -none- numeric
aliased 2 -none- logical
sigma 1 -none- numeric
df 3 -none- numeric
r.squared 1 -none- numeric
adj.r.squared 1 -none- numeric
fstatistic 3 -none- numeric
cov.unscaled 4 -none- numeric
>
:これらか変数名らしい。
:個々の変数には、JavaのMap,Listみたいな構造があるようだ、、、
:Pythonもリスト構造が好きだったなぁ、、、
--------------
単回帰分析結果の抜き出し方は以下。
> result = summary(result <- lm(LN_jika ~ LN_order, data = foods))
> result
Call:
lm(formula = LN_jika ~ LN_order, data = foods)
Residuals:
Min 1Q Median 3Q Max
-1.3499 -0.2369 0.1748 0.2605 0.5010
Coefficients:
Estimate Std. Error t value Pr(>t)
(Intercept) 22.58154 0.19806 114.02 <2e-16 ***
LN_order -1.45403 0.05825 -24.96 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4366 on 67 degrees of freedom
Multiple R-squared: 0.9029, Adjusted R-squared: 0.9015
F-statistic: 623.1 on 1 and 67 DF, p-value: < 2.2e-16
>
---
> result$r.squared
[1] 0.9029087
> result$r.squared[1]
[1] 0.9029087
> result$coefficients
Estimate Std. Error t value Pr(>t)
(Intercept) 22.581540 0.19805779 114.01490 1.864877e-78
LN_order -1.454033 0.05825122 -24.96141 1.200399e-35
> result$coefficients[1]
[1] 22.58154
> result$coefficients[2]
[1] -1.454033
> result$coefficients[3]
[1] 0.1980578
> result$coefficients[4]
[1] 0.05825122
> result$coefficients[5]
[1] 114.0149
> result$coefficients[6]
[1] -24.96141
> result$coefficients[7]
[1] 1.864877e-78
> result$coefficients[8]
[1] 1.200399e-35
>
---
データから、線形近似の切片、傾きとR^2(決定係数=R-Squared)を割り出す最終形は以下。
foods = read.csv("20081212-foods.txt");
result = summary(result <- lm(LN_jika ~ LN_order, data = foods))
result$r.squared
result$coefficients[1]
result$coefficients[2]
結果は?
> result$r.squared
[1] 0.9029087
> result$coefficients[1]
[1] 22.58154
> result$coefficients[2]
[1] -1.454033
>
~~~
end
0 件のコメント:
コメントを投稿