2015-01-06 48 views
-1

我需要對常數運行迴歸。在Eviews中,當我在常量上運行迴歸時,我不需要將任何事物作爲預測變量。我不知道如何在R中執行此操作。是否有人知道我應該在此函數中寫些什麼?迴歸常數

適合= LM(R〜?)

回答

1

您可以在公式中指定一個常數1

r <- 1:5 
fit <- lm(r ~ 1) 
summary(fit) 
# Call: 
# lm(formula = r ~ 1) 
# 
# Residuals: 
# 1   2   3   4   5 
# -2.00e+00 -1.00e+00 2.22e-16 1.00e+00 2.00e+00 
# 
# Coefficients: 
#    Estimate Std. Error t value Pr(>|t|) 
# (Intercept) 3.0000  0.7071 4.243 0.0132 * 
# --- 
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
# 
# Residual standard error: 1.581 on 4 degrees of freedom 

注意,你不需要lm得到這樣的結果:

mean(r) 
#[1] 3 
sd(r)/sqrt(length(r)) 
#[1] 0.7071068 

但是,您可能想要使用lm以便擁有一個可以與其他模型進行比較的空模型...

編輯:

既然你評論,你需要「p值」,我建議使用t檢驗來代替。

t.test(r) 
# One Sample t-test 
# 
#data: r 
#t = 4.2426, df = 4, p-value = 0.01324 
#alternative hypothesis: true mean is not equal to 0 
#95 percent confidence interval: 
# 1.036757 4.963243 
#sample estimates: 
#mean of x 
#  3 

這是等價的,但計算效率更高。

+0

非常感謝您的回答。是的,我需要看看這種迴歸的P值,這就是我爲什麼要這樣做的原因。 – Rfinance

+0

@Rfinance看我的編輯。 – Roland

+0

謝謝,我將它改爲t檢驗。結果大致相同。 – Rfinance