Rfinance
Rfinance

Reputation: 21

Regression on constant

I need to run a regression on a constant. In Eviews, I don't need to put any thing as a predictor when I run a regression on constant.I don't know how to do that in R. Does any one knows what should I write in this commnd?

fit= lm(r~?)

Upvotes: 0

Views: 4668

Answers (1)

Roland
Roland

Reputation: 132959

You can specify a constant as 1 in a formula:

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

Note that you don't need lm to get this result:

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

However, you might want to use lm in order to have a Null model against which you can compare other models ...

Edit:

Since you comment that you need "the p-value", I suggest to use a t-test instead.

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

This is equivalent, but more efficient computationally.

Upvotes: 1

Related Questions