2017-10-17 4 views
0

わかりました。私は、ダミーや要因によって2つの問題があります。私は私のデータベースによく似た例を使用します。 1つの国の大統領(たとえば、「George W.」、「Bill C.」など)という名前の列が20列あります。また、25列の戦略(例:「str_1」、「str2」など)があります。それらはすべて同じデータベースにあります。例えば、 "dat"は、yやxのような他の変数と一緒にあります。R回帰における因子とダミー

============================= 
y x presidents strategies 
============================ 
20 2 Bill.C  3_A 
10 1 George.W 2_B 
10 1 Tom_C  3_C 
3 2 Tom_C  2_D 
4 4 John.C  3_A 
4 3 Bill.C  2_A 

私は戦略+大統領と戦略の間の相互作用のための大統領+ダミーのためのy〜X +ダミーを退行したいと思います。

私はすでに20の大統領と25の戦略のそれぞれについてダミーを作成しましたが、私は各大統領と各戦略(これは私の問題の最初の部分です)とのやりとりをどのように作成するのか分かりません。私はこれを簡単に行うことができると仮定して、20 * 25回の対話を1つずつ書く必要なしに回帰を指定する他の方法はありますか(Stataはこの同じ問題に対していくつかのコマンドを持っています)。

多分それらは別の質問ですが、わかりません。

ありがとうございます。

+0

このデータベース内の行は何ですか? –

+1

"私の回帰を指定するのに、20 * 25個の対話を1つずつ書き込まなくても指定できる他の方法はありますか?"はいあります。 'lm'は因子変数を自動的に対応するダミーに変換します(参照カテゴリとして残します)。だから 'lm(y〜x +大統領+戦略+大統領:戦略、データ= dat)'と書くだけで十分です。同じ仕様。 – useR

+0

より大きいデータセットを提供する必要があります。なぜなら、OLSでは、観測よりも変数の多いデータセット(ダミーやインタラクションを含む場合)を処理できないためです。 – useR

答えて

0

lmおよびglmは、因子変数を対応するダミーに自動的に変換します(参照カテゴリとして残します)。だから、次の操作を実行するのに十分だ:

mod1 = lm(y ~ x + presidents + strategies + presidents:strategies, data = df1) 
mod2 = lm(y ~ x + presidents*strategies, data = df1) 
mod3 = glm(y ~ x + presidents + strategies + presidents:strategies, data = df1) 
mod4 = glm(y ~ x + presidents*strategies, data = df1) 

summary(mod1) 
summary(mod2) 
summary(mod3) 
summary(mod4) 

結果:あなたが見ることができるように

> summary(mod1) 

Call: 
lm(formula = y ~ x + presidents + strategies + presidents:strategies, 
    data = df1) 

Residuals: 
    Min  1Q Median  3Q  Max 
-17.3690 -6.1273 -0.1699 6.4295 17.4156 

Coefficients: 
           Estimate Std. Error t value Pr(>|t|)  
(Intercept)      14.4782  3.0799 4.701 5.15e-06 *** 
x         -0.1692  0.2141 -0.790 0.431  
presidentsGeorge.W    11.1984  8.8283 1.268 0.206  
presidentsJohn.C     4.1281  4.2305 0.976 0.330  
presidentsTom_C     4.9604  3.6271 1.368 0.173  
strategies2_B      1.6203  3.5736 0.453 0.651  
strategies2_D      -1.7246  3.6550 -0.472 0.638  
strategies3_A      1.7663  3.2966 0.536 0.593  
strategies3_C      -0.5787  3.8440 -0.151 0.881  
presidentsGeorge.W:strategies2_B -9.9934 10.0125 -0.998 0.320  
presidentsJohn.C:strategies2_B -1.5192  5.8696 -0.259 0.796  
presidentsTom_C:strategies2_B  -0.8962  5.0202 -0.179 0.859  
presidentsGeorge.W:strategies2_D -7.5266  9.7414 -0.773 0.441  
presidentsJohn.C:strategies2_D  1.7179  6.4375 0.267 0.790  
presidentsTom_C:strategies2_D  -1.1020  5.0551 -0.218 0.828  
presidentsGeorge.W:strategies3_A -11.9783  9.3115 -1.286 0.200  
presidentsJohn.C:strategies3_A -2.8849  5.0866 -0.567 0.571  
presidentsTom_C:strategies3_A  -5.0305  4.4068 -1.142 0.255  
presidentsGeorge.W:strategies3_C -6.5116  9.7387 -0.669 0.505  
presidentsJohn.C:strategies3_C -4.3792  6.0389 -0.725 0.469  
presidentsTom_C:strategies3_C  -1.3257  5.3821 -0.246 0.806  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 8.364 on 179 degrees of freedom 
Multiple R-squared: 0.064, Adjusted R-squared: -0.04058 
F-statistic: 0.612 on 20 and 179 DF, p-value: 0.9007 

> summary(mod2) 

Call: 
lm(formula = y ~ x + presidents * strategies, data = df1) 

Residuals: 
    Min  1Q Median  3Q  Max 
-17.3690 -6.1273 -0.1699 6.4295 17.4156 

Coefficients: 
           Estimate Std. Error t value Pr(>|t|)  
(Intercept)      14.4782  3.0799 4.701 5.15e-06 *** 
x         -0.1692  0.2141 -0.790 0.431  
presidentsGeorge.W    11.1984  8.8283 1.268 0.206  
presidentsJohn.C     4.1281  4.2305 0.976 0.330  
presidentsTom_C     4.9604  3.6271 1.368 0.173  
strategies2_B      1.6203  3.5736 0.453 0.651  
strategies2_D      -1.7246  3.6550 -0.472 0.638  
strategies3_A      1.7663  3.2966 0.536 0.593  
strategies3_C      -0.5787  3.8440 -0.151 0.881  
presidentsGeorge.W:strategies2_B -9.9934 10.0125 -0.998 0.320  
presidentsJohn.C:strategies2_B -1.5192  5.8696 -0.259 0.796  
presidentsTom_C:strategies2_B  -0.8962  5.0202 -0.179 0.859  
presidentsGeorge.W:strategies2_D -7.5266  9.7414 -0.773 0.441  
presidentsJohn.C:strategies2_D  1.7179  6.4375 0.267 0.790  
presidentsTom_C:strategies2_D  -1.1020  5.0551 -0.218 0.828  
presidentsGeorge.W:strategies3_A -11.9783  9.3115 -1.286 0.200  
presidentsJohn.C:strategies3_A -2.8849  5.0866 -0.567 0.571  
presidentsTom_C:strategies3_A  -5.0305  4.4068 -1.142 0.255  
presidentsGeorge.W:strategies3_C -6.5116  9.7387 -0.669 0.505  
presidentsJohn.C:strategies3_C -4.3792  6.0389 -0.725 0.469  
presidentsTom_C:strategies3_C  -1.3257  5.3821 -0.246 0.806  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 8.364 on 179 degrees of freedom 
Multiple R-squared: 0.064, Adjusted R-squared: -0.04058 
F-statistic: 0.612 on 20 and 179 DF, p-value: 0.9007 

> summary(mod3) 

Call: 
glm(formula = y ~ x + presidents + strategies + presidents:strategies, 
    data = df1) 

Deviance Residuals: 
    Min  1Q Median  3Q  Max 
-17.3690 -6.1273 -0.1699 6.4295 17.4156 

Coefficients: 
           Estimate Std. Error t value Pr(>|t|)  
(Intercept)      14.4782  3.0799 4.701 5.15e-06 *** 
x         -0.1692  0.2141 -0.790 0.431  
presidentsGeorge.W    11.1984  8.8283 1.268 0.206  
presidentsJohn.C     4.1281  4.2305 0.976 0.330  
presidentsTom_C     4.9604  3.6271 1.368 0.173  
strategies2_B      1.6203  3.5736 0.453 0.651  
strategies2_D      -1.7246  3.6550 -0.472 0.638  
strategies3_A      1.7663  3.2966 0.536 0.593  
strategies3_C      -0.5787  3.8440 -0.151 0.881  
presidentsGeorge.W:strategies2_B -9.9934 10.0125 -0.998 0.320  
presidentsJohn.C:strategies2_B -1.5192  5.8696 -0.259 0.796  
presidentsTom_C:strategies2_B  -0.8962  5.0202 -0.179 0.859  
presidentsGeorge.W:strategies2_D -7.5266  9.7414 -0.773 0.441  
presidentsJohn.C:strategies2_D  1.7179  6.4375 0.267 0.790  
presidentsTom_C:strategies2_D  -1.1020  5.0551 -0.218 0.828  
presidentsGeorge.W:strategies3_A -11.9783  9.3115 -1.286 0.200  
presidentsJohn.C:strategies3_A -2.8849  5.0866 -0.567 0.571  
presidentsTom_C:strategies3_A  -5.0305  4.4068 -1.142 0.255  
presidentsGeorge.W:strategies3_C -6.5116  9.7387 -0.669 0.505  
presidentsJohn.C:strategies3_C -4.3792  6.0389 -0.725 0.469  
presidentsTom_C:strategies3_C  -1.3257  5.3821 -0.246 0.806  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

(Dispersion parameter for gaussian family taken to be 69.96038) 

    Null deviance: 13379 on 199 degrees of freedom 
Residual deviance: 12523 on 179 degrees of freedom 
AIC: 1439 

Number of Fisher Scoring iterations: 2 

> summary(mod4) 

Call: 
glm(formula = y ~ x + presidents * strategies, data = df1) 

Deviance Residuals: 
    Min  1Q Median  3Q  Max 
-17.3690 -6.1273 -0.1699 6.4295 17.4156 

Coefficients: 
           Estimate Std. Error t value Pr(>|t|)  
(Intercept)      14.4782  3.0799 4.701 5.15e-06 *** 
x         -0.1692  0.2141 -0.790 0.431  
presidentsGeorge.W    11.1984  8.8283 1.268 0.206  
presidentsJohn.C     4.1281  4.2305 0.976 0.330  
presidentsTom_C     4.9604  3.6271 1.368 0.173  
strategies2_B      1.6203  3.5736 0.453 0.651  
strategies2_D      -1.7246  3.6550 -0.472 0.638  
strategies3_A      1.7663  3.2966 0.536 0.593  
strategies3_C      -0.5787  3.8440 -0.151 0.881  
presidentsGeorge.W:strategies2_B -9.9934 10.0125 -0.998 0.320  
presidentsJohn.C:strategies2_B -1.5192  5.8696 -0.259 0.796  
presidentsTom_C:strategies2_B  -0.8962  5.0202 -0.179 0.859  
presidentsGeorge.W:strategies2_D -7.5266  9.7414 -0.773 0.441  
presidentsJohn.C:strategies2_D  1.7179  6.4375 0.267 0.790  
presidentsTom_C:strategies2_D  -1.1020  5.0551 -0.218 0.828  
presidentsGeorge.W:strategies3_A -11.9783  9.3115 -1.286 0.200  
presidentsJohn.C:strategies3_A -2.8849  5.0866 -0.567 0.571  
presidentsTom_C:strategies3_A  -5.0305  4.4068 -1.142 0.255  
presidentsGeorge.W:strategies3_C -6.5116  9.7387 -0.669 0.505  
presidentsJohn.C:strategies3_C -4.3792  6.0389 -0.725 0.469  
presidentsTom_C:strategies3_C  -1.3257  5.3821 -0.246 0.806  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

(Dispersion parameter for gaussian family taken to be 69.96038) 

    Null deviance: 13379 on 199 degrees of freedom 
Residual deviance: 12523 on 179 degrees of freedom 
AIC: 1439 

Number of Fisher Scoring iterations: 2 

、推定値はまったく同じです。

データ:

df = read.table(text = "y x presidents strategies 
       20 2 Bill.C  3_A 
       10 1 George.W 2_B 
       10 1 Tom_C  3_C 
       3 2 Tom_C  2_D 
       4 4 John.C  3_A 
       4 3 Bill.C  2_A", header = TRUE) 

set.seed(123) 
df1 = data.frame(y = sample(1:30, 200, replace = TRUE), 
       x = sample(1:10, 200, replace = TRUE), 
       presidents = sample(df$presidents, 200, replace = TRUE), 
       strategies = sample(df$strategies, 200, replace = TRUE)) 
+0

ありがとうございました。 – RandomWalker

+0

@RandomWalkerこの回答が役に立ちましたら、downvoteボタンの下にある灰色のチェックマークをクリックして受け入れてください。 – useR

+0

もちろん。私が間違っていなければ、私は投票しました。 :) – RandomWalker