Wenn ich Ihre Frage richtig verstanden habe, ist die typische Methode zum Hinzufügen von Dummy-Variablen (auch Fixed-Effekte genannt) die Verwendung der Funktion factor
. Hier ist ein ein Beispiel, das Zufallsdaten erzeugt und verwendet dann factor
in einer linearen Regression:
set.seed(1)
require(data.table)
A = data.table(region = LETTERS[0:3], y = runif(100), x = runif(100), mafia = sample(c(0,1),100,rep = T))
> head(A)
region var mafia
1: A 0.67371223 1
2: B 0.09485786 0
3: C 0.49259612 1
4: A 0.46155184 1
5: B 0.37521653 1
6: C 0.99109922 1
formula = y ~ x + factor(mafia)
reg <- lm(formula, data = A)
> summary(reg)
Call:
lm(formula = formula, data = A)
Residuals:
Min 1Q Median 3Q Max
-0.46965 -0.24828 -0.03362 0.28780 0.51183
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.46196 0.07093 6.513 3.28e-09 ***
x 0.06735 0.10521 0.640 0.524
factor(mafia)1 -0.01830 0.06415 -0.285 0.776
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.3189 on 97 degrees of freedom
Multiple R-squared: 0.005498, Adjusted R-squared: -0.01501
F-statistic: 0.2681 on 2 and 97 DF, p-value: 0.7654
Wenn Sie möchten nur auf den Beobachtungen eine Regression tun, die mit 1 in der „Mafia“ Spalte codiert sind, ist dies viel einfacher:
# Note that A is a data.table
A.mafia = A[ mafia == 1 ]
formula = y ~ x
reg <- lm(formula, data = A.mafia)
summary(reg)
Ausgang:
Call:
lm(formula = formula, data = A.mafia)
Residuals:
Min 1Q Median 3Q Max
-0.47163 -0.26063 -0.05724 0.30166 0.52062
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.43334 0.07926 5.467 1.53e-06 ***
x 0.09017 0.14474 0.623 0.536
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.3197 on 49 degrees of freedom
Multiple R-squared: 0.007857, Adjusted R-squared: -0.
F-statistic: 0.388 on 1 and 49 DF, p-value: 0.5362
Willkommen bei Stackoverflow. Bitte werfen Sie einen Blick auf diese Tipps, wie Sie ein [minimales, vollständiges und überprüfbares Beispiel] (http://stackoverflow.com/help/mcve) erstellen können, sowie auf diesen Post zu [ein großartiges Beispiel in R erstellen] (http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Vielleicht sind auch die folgenden Tipps zu [eine gute Frage stellen] (http://stackoverflow.com/help/how-to-ask) lohnenswert. – lmo