2017-12-14 6 views
1

私のデータ(パッケージ 'ecm')の時系列エラー修正モデルを推定しています。以下のコードでは、xeqとxtrを使って短期変数と長期変数を指定しています。回帰回帰推定(ECMモデル)の場合

これらの変数は独立変数であり、従属変数の推定値です。

この場合、プールされたモデルですが、このモデル単位を単位(各ブランドごとに別々に)で見積もりたいと思います。私のデータセットはむしろ大きく、それぞれ3つのブランド(ブランド2、ブランド3、ブランド4)を持つ360の製品カテゴリーで構成されています。

xeq <- DatasetThesisSynergyClean[c('lnPrice', 'lnAdvertising', 'lnDisplay', 'IntrayearCycles', 'lnCompetitorPrices', 'lnCompADV', 'lnCompDISP' , 'ADVxDISP', 'ADVxCYC', 'DISPxCYC', 'ADVxDISPxCYC')]  
xtr <- DatasetThesisSynergyClean[c('lnPrice', 'lnAdvertising', 'lnDisplay', 'IntrayearCycles', 'lnCompetitorPrices', 'lnCompADV', 'lnCompDISP', 'ADVxDISP', 'ADVxCYC', 'DISPxCYC', 'ADVxDISPxCYC')]  
model11 <- ecm(DatasetThesisSynergyClean$lnSales, xeq, xtr, includeIntercept=TRUE) 
summary(model11) 

私が望むのは、すべてのカテゴリのすべてのブランドの出力を生成することです。私のデータを垣間見るあなたを与えるために、このコードを実行してください:あなたが見ることができるように

structure(list(Week = 7:17, Category = c("2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2"), Brand = c("3", "3", "3", 
"3", "3", "3", "3", "3", "3", "3", "3"), Display = c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), Sales = c(0, 0, 0, 0, 13.440948, 40.097397, 
32.01384, 382.169189, 2830.748779, 4524.460938, 1053.590576), 
    Price = c(0, 0, 0, 0, 5.949999, 5.95, 5.950003, 4.87759, 
    3.787015, 3.205987, 4.898724), Distribution = c(0, 0, 0, 
    0, 1.394019, 1.386989, 1.621416, 8.209759, 8.552915, 9.692097, 
    9.445554), Advertising = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0), lnSales = c(11.4945151554497, 11.633214247508, 11.5862944141137, 
    11.5412559646132, 11.4811122484454, 11.4775106999991, 11.6333660772506, 
    11.4859819773102, 11.5232680456161, 11.5572670584292, 11.5303686934256 
    ), IntrayearCycles = c(4.15446534315765, 3.62757053512638, 
    2.92387946552647, 2.14946414386239, 1.40455011205262, 0.768856938870769, 
    0.291497141953598, -0.0131078404184544, -0.162984144025091, 
    -0.200882782749248, -0.182877633924882), `Competitor Advertising` = c(10584.87063, 
    224846.3243, 90657.72553, 0, 0, 0, 2396.54212, 0, 0, 0, 40343.49444 
    ), `Competitor Display` = c(0.385629, 2.108133, 2.515806, 
    4.918288, 3.81749, 3.035847, 2.463194, 3.242594, 1.850399, 
    1.751096, 1.337943), `Competitor Prices` = c(5.30989, 5.372752, 
    5.3717245, 5.3295525, 5.298393, 5.319466, 5.1958415, 5.2941095, 
    5.296757, 5.294059, 5.273578), ZeroSales = c(1, 1, 1, 1, 
    0, 0, 0, 0, 0, 0, 0)), .Names = c("Week", "Category", "Brand", 
"Display", "Sales", "Price", "Distribution", "Advertising", "lnSales", 
"IntrayearCycles", "Competitor Advertising", "Competitor Display", 
"Competitor Prices", "ZeroSales"), row.names = 1255:1265, class = "data.frame") 

、私は列に格納されているすべてのカテゴリやブランドを持っています。すべての単一のブランドについての見積もりを得るには、forループを作成したいが、この出力を別々に保存するには、正しいカテゴリとブランドを指定する方法がわからない。

最終的に係数を保存したい、std。エラー、t値およびp値を4つの別々のデータフレームのすべてのブランドに適用します。しかし、まず出力を得る必要があります。皆さんが私を助けてくれますか?

+0

あります'dplyr'、' 'purr''パッケージを使って各ブランドのモデルを構築する別の方法です。線形回帰モデルと関係がある多くの記事がオンラインにあります。あなたはあなたのケースにそれを調整することができるはずです。いくつかの情報はhttps://www.r-bloggers.com/running-a-model-on-separate-groups/こちらをご覧ください。https://stackoverflow.com/questions/38621556/advice-on-usage-of- dplyr-do-vs-purrr-map-tidynest-for-predictions – AntoniosK

答えて

0

あなたはこのようdplyrを使用することができます。

f <- function(.) { 
    xeq <- as.data.frame(select(., lnPrice, lnAdvertising, lnDisplay, IntrayearCycles, lnCompetitorPrices, lnCompADV, lnCompDISP, ADVxDISP, ADVxCYC, DISPxCYC, ADVxDISPxCYC)) 
    xtr <- as.data.frame(select(., lnPrice, lnAdvertising, lnDisplay, IntrayearCycles, lnCompetitorPrices, lnCompADV, lnCompDISP, ADVxDISP, ADVxCYC, DISPxCYC, ADVxDISPxCYC)) 
    print(xeq) 
    print(xtr) 
    summary(ecm(.$lnSales, xeq, xtr, includeIntercept = TRUE)) 
} 


Models <- DatasetThesisSynergyClean %>% 
    group_by(Category, Brand) %>% 
    do(Model = f(.)) 


Models$Category 
[1] "2" "3" 
Models$Brand 
[1] "3" "3" 
Models$Model 
[[1]] 

Call: 
lm(formula = dy ~ ., data = x) 
# ... and so on 

あなたは3つの項目(カテゴリ、ブランドとモデルの概要オブジェクト)と独自のカテゴリ/ブランドの組み合わせに等しい長さのリストで終わります。私は完全なデータを持っていないので、それを正しく試すことができませんでした。カテゴリ3のモデルの概要、ブランド3:

Models$Model[[which(Models$Category == 3 & Models$Brand == 3)]] 

更新:

あなたは、各モデルのスタンドアロンオブジェクトをしたい場合は、それらに対応する名前を与えるとlist2env()を使用することができます。

names(Models$Model) <- paste0("C", Models$Category, "B", Models$Brand) 
list2env(Models$Model, .GlobalEnv) 
+0

これは動作するようです!私はDFの 'モデル'を見ることができ、私が最後の3行を実行すると、コンソールにすべての出力が表示されます。 1つの特定のカテゴリとブランドのモデル出力をどのように取得する必要があるのか​​分かりません。 – PimM

+0

まあ 'モデル$モデル[[1]]はCat 2、ブランド3に対応しています...'モデル$カテゴリ[1] 'と'モデル$ブランド[1] ' –

+0

これはあなたができます:'モデル$モデル[[(モデル$カテゴリ== 3&モデル$ブランド== 3)]] ' –

0

tidyverseパッケージのいくつかを見て、split(df, list(df$Category, df$Group))とpurrrのmap()関数を組み合わせたベクトル化アプローチを使用して、小さなデータセットのそれぞれに関数を適用することをお勧めします。コードは次のようなものになります。

df %>% 
    split(f = list(.$Category, .$Brand)) %>% 
    map(a_function_for_each_group) %>% 
    bind_rows() 

私はあなたの質問を正しく理解してくれることを望みます。

+0

私はこれを適用することができましたが、それ以上はどうしたらいいですか? – PimM

+0

'a_function_for_each_group'出力の構造と内容を知らずに進める方法を示唆することは難しいです。 –

関連する問題