2015-01-09 72 views
1
d = data.frame(
    Temperature = c(rep("Cool", 6), rep("Warm", 6)), 
    Bact = c(rep("Bact 1", 2), rep("Bact 2", 2), rep("Bact 3", 2), rep("Bact 1", 2), rep("Bact 2", 2), rep("Bact 3", 2)), 
    Time = c(15.23,14.32,14.77,15.12,14.05,15.48,14.13,16.13,16.44,14.82,17.96,16.65) 
) 

我自行創建了一個雙向ANOVA的小數據框。我想創建虛擬變量做雙向ANOVA

summary(aov(Time~Bact*Temperature, data=d)) 

時間進行雙因素方差分析模型是因變量,而BACT和溫度兩個分類獨立變量。

我不想用ANOVA的方式來做,我想學習和證明ANOVA也可以用線性迴歸模型來完成。我想將我的數據轉換爲虛擬變量並對其執行線性迴歸。我希望我能恢復相同的結果。虛擬變量還將包括Bact和溫度之間的交互作用。

問題是,我不知道如何將我的數據框轉換爲虛擬變量,以便它可以在lm()函數中使用。

回答

1

我有種做同樣的和你。我想控制的感覺所以每當我有時間我設計假人自己有以下幾點:

d = data.frame(
    Temperature = c(rep("Cool", 6), rep("Warm", 6)), 
    Bact = c(rep("Bact 1", 2), rep("Bact 2", 2), rep("Bact 3", 2), rep("Bact 1", 2), rep("Bact 2", 2), rep("Bact 3", 2)), 
    Time = c(15.23,14.32,14.77,15.12,14.05,15.48,14.13,16.13,16.44,14.82,17.96,16.65) 
) 

是:

> d 
    Temperature Bact Time 
1   Cool Bact 1 15.23 
2   Cool Bact 1 14.32 
3   Cool Bact 2 14.77 
4   Cool Bact 2 15.12 
5   Cool Bact 3 14.05 
6   Cool Bact 3 15.48 
7   Warm Bact 1 14.13 
8   Warm Bact 1 16.13 
9   Warm Bact 2 16.44 
10  Warm Bact 2 14.82 
11  Warm Bact 3 17.96 
12  Warm Bact 3 16.65 

所以你只需要dummify因素(溫度,BACT),所以以下過程的工作:

xfactors <- Filter(is.factor,d) #filter only the factors to dummify 
b <- data.frame(matrix(NA,nrow=nrow(xfactors),ncol=1)) #make empty data.frame to initiate b 
for (i in 1:ncol(xfactors)) { #start loop 
    a <- data.frame(model.matrix(~xfactors[,i])) #make dummies here 
    b <- cbind(b, a[-1]) #remove intercept and combine dummies 
} 
b <- data.frame(b[-1]) #make a data.frame 
#the reference dummy gets excluded automatically by model.matrix 
colnames(b) <- c('warm' , 'bact2' , 'bact3') #you will probably want to change the names to sth smaller 

> b 
    warm bact2 bact3 
1  0  0  0 
2  0  0  0 
3  0  1  0 
4  0  1  0 
5  0  0  1 
6  0  0  1 
7  1  0  0 
8  1  0  0 
9  1  1  0 
10 1  1  0 
11 1  0  1 
12 1  0  1 

然後運行模型:

new_data <- cbind(b, Time=d$Time) #add time to the data 
mymod <- lm(Time ~ warm*bact2+warm*bact3, data=new_data) #compute lm with interactions 
#you shouldn't compute the interactions between dummy variables because they come from the same variable 

輸出:

> summary(mymod) 

Call: 
lm(formula = Time ~ warm * bact2 + warm * bact3, data = new_data) 

Residuals: 
    Min  1Q Median  3Q Max 
-1.00 -0.67 0.00 0.67 1.00 

Coefficients: 
      Estimate Std. Error t value Pr(>|t|)  
(Intercept) 14.7750  0.6873 21.498 6.61e-07 *** 
warm   0.3550  0.9719 0.365 0.727  
bact2   0.1700  0.9719 0.175 0.867  
bact3  -0.0100  0.9719 -0.010 0.992  
warm:bact2 0.3300  1.3745 0.240 0.818  
warm:bact3 2.1850  1.3745 1.590 0.163  
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.9719 on 6 degrees of freedom 
Multiple R-squared: 0.6264, Adjusted R-squared: 0.3151 
F-statistic: 2.012 on 5 and 6 DF, p-value: 0.2097 
+1

'溫暖* bact2' =='溫暖+ bact2 +溫暖:bact2'。添加另一個'warm * bact3'確實會增加'warm'兩次,但是R足夠聰明,可以排除具有相同名稱的多個變量實例並且只包含一次。嘗試'lm(時間〜溫暖+溫暖+溫暖,數據=新數據)',並看看你會得到多少係數:) – LyzandeR

0

lm()將爲您創建虛擬變量。無需自己創建它們:

m <- lm(Time ~ Bact*Temperature, data = d) 
anova(m) 

編輯

如果你想的lm()引擎蓋下同行,你可以看到設計矩陣model.matrix(m)

+0

這是一個自學問題。我有興趣學習統計數據和自己做的骯髒工作,否則R函數會隱藏這些工作。 – SmallChess

+0

@StudentT如果你想生成「X」矩陣,你可以使用'model.matrix(Time-Bact * Temperature,data = d)',但是你不能使用這樣的矩陣作爲輸入到'lm'了。要R只爲你做一半的工作是很奇怪的。 – MrFlick