2017-11-12 10 views
0

私は、lagcolmeanとDropcolmaxの2つのデータフレームを持っています。ここで、rownamesは企業で、columnnamesは月の日付です。多くの企業が横断する回帰式

     00-02 00-03 00-04 
TENAGA NASIONAL  0.39 0.07 -0.08 
SIME DARBY   -0.09 -0.12 -0.53 
DIGI.COM    0.79 0.96 -1.14 
GENTING    -0.11 -0.27 -0.16 
PETRONAS GAS  -0.30 -0.09 -0.98 
and 
        00-01 00-02 00-03 
TENAGA NASIONAL  5.61 3.95 4.12 
SIME DARBY   10.87 1.97 6.78 
DIGI.COM   21.21 9.61 25.40 
GENTING   11.55 2.87 4.34 
PETRONAS GAS  1.79 1.27 4.75 

私はそれがこのエラーメッセージを生成し

library(broom) 
fit4 <- lapply(names(Dropcolmax), function(x){ 
    dd = tidy(lm(lagcolmean[[x]] ~ Dropcolmax[[x]])) 
    data.frame(name = x, dd)}) 

しかし、これらの式を使用し、各期間のスロープ係数を見つけるために、断面回帰を実行したいときは:model.frame.defaultでのエラーを(式= lagcolmean [[X]]〜Dropcolmax [[X]]、: 無効な型の変数のための(NULL)

答えて

0

rownamesに変更し、あなたの名前をし、自分のLM仕様 '[[X]] lagcolmean':

fit4 <- lapply(rownames(Dropcolmax), function(x){ 
    dd = tidy(lm(as.numeric(lagcolmean[rownames(lagcolmean)==x,]) ~ as.numeric(Dropcolmax[rownames(Dropcolmax)==x,]))) 
    data.frame(name = x, dd)}) 

あなたが(行単位で)取得します:

> fit4 
[[1]] 
      name            term estimate std.error statistic p.value 
1 TENAGA NASIONAL           (Intercept) -0.9721944 0.4851794 -2.003783 0.2946863 
2 TENAGA NASIONAL as.numeric(Dropcolmax[rownames(Dropcolmax) == x, ]) 0.2409783 0.1050042 2.294939 0.2616084 

[[2]] 
     name            term  estimate std.error statistic p.value 
1 SIME DARBY           (Intercept) -0.2518569598 0.41291644 -0.60994656 0.6513227 
2 SIME DARBY as.numeric(Dropcolmax[rownames(Dropcolmax) == x, ]) 0.0007936228 0.05517726 0.01438315 0.9908440 

編集2:あなたは、列

indc=names(Dropcolmax) %in% names(lagcolmean) 
fit5 <- lapply(names(Dropcolmax)[indc], function(x){ 
    df=data.frame(lagcolmean[x] , Dropcolmax[x]) 
    dd = tidy(lm(df[,1]~df[,2])) 
    data.frame(name = x, dd)}) 

で結果をしたい場合は、取得します:

> fit5 
[[1]] 
    name  term estimate std.error statistic p.value 
1 X00.02 (Intercept) -0.3597760 0.12858456 -2.797972 0.06796718 
2 X00.02  df[, 2] 0.1260234 0.02606482 4.834999 0.01687099 

[[2]] 
    name  term estimate std.error statistic p.value 
1 X00.03 (Intercept) -0.3545013 0.107404102 -3.300631 0.04571181 
2 X00.03  df[, 2] 0.0511678 0.008772428 5.832799 0.01003905 
+0

あなたはこのことを意味; ライブラリ(broom) fit4 < - lapply(名前(Dropcolmax)、関数(x){ dd = tidy(lmcolmean [rownames(lagcolmean)== x]〜Dropcolmax [rownames(Dropcolmax)== x]) ) data.frame(name = x、dd)})まだ動作していません –

+0

ありがとうございました。しかし、アウトプットは各企業の時系列回帰である勾配と切片係数を示しています。しかし、私は各期間の傾きと切片の係数を求めます。周期00-02のスロープと同様に00-03 @Robertのスロープ –

関連する問題