2017-02-02 30 views
0

我有關於在R.估計迴歸模型問題我有以下的數據(例如):更改在迴歸模型的對比,其中R

Year XY 
2002 5 
2003 2 
2004 4 
2005 8 
2006 3 
2007 5 
2008 10 

the regression model I want to estimate is: 
XY = B0 + Y2005 + Y2006 + Y2007 + Y2008 + e 

凡Y2005,Y2006,Y2007,Y2008和每年都指標變量在2005年,2006年,2007年和2008年取值爲1,否則爲0。

我需要做的是比較2005年,2006年,2007年和2008年的(XY)值與(2002-2004)期​​間的(XY)的平均值。

我希望你能幫我弄清楚這個問題,並提前感謝你的幫助。

回答

0
DF <- read.table(text = "Year XY 
       2002 5 
       2003 2 
       2004 4 
       2005 8 
       2006 3 
       2007 5 
       2008 10", header = TRUE) 

DF$facYear <- DF$Year 
DF$facYear[DF$facYear < 2005] <- "baseline" 
DF$facYear <- factor(DF$facYear) 

#make sure that baseline is used as intercept: 
DF$facYear <- relevel(DF$facYear, "baseline") 

fit <- lm(XY ~ facYear, data = DF) 
summary(fit) 
#Coefficients: 
#   Estimate Std. Error t value Pr(>|t|) 
#(Intercept) 3.6667  0.8819 4.158 0.0533 . 
#facYear2005 4.3333  1.7638 2.457 0.1333 
#facYear2006 -0.6667  1.7638 -0.378 0.7418 
#facYear2007 1.3333  1.7638 0.756 0.5286 
#facYear2008 6.3333  1.7638 3.591 0.0696 . 
+0

謝謝@羅蘭的幫助 –