top of page

From: Bayesian Models for Astrophysical Data, Cambridge Univ. Press

(c) 2017,  Joseph M. Hilbe, Rafael S. de Souza and Emille E. O. Ishida  

 

you are kindly asked to include the complete citation if you used this material in a publication

​

Code 2.1 Example of linear regression in R.
==================================================
# Data
y   <- c(13,15,9,17,8,5,19,23,10,7,10,6)                              # continuous response variable
x1 <- c(1,1,1,1,1,1,0,0,0,0,0,0)                                            # binary predictor
x2 <- c( 1,1,1,1,2,2,2,2,3,3,3,3)                                           # categorical predictor


# Fit
mymodel <- lm(y ~ x1 + x2)                                                # linear regression of y on x1 and x2


# Output
summary(mymodel)                                                            # summary display
par(mfrow=c(2, 2))                                                              # create a 2 by 2 window
plot(mymodel)                                                                     # display of fitted vs. residuals plot, normal QQ plot
                                                                                             # scale-location plot and residuals vs. leverage plot
==================================================

Output on screen:

​

Call:

lm(formula = y ~ x1 + x2)

 

Residuals:

 

      Min           1Q    Median        3Q         Max

-5.4583    -1.6458    0.4792    1.2292    3.9167

 

Coefficients:

                     Estimate Std.    Error    t value    Pr(>|t|)

(Intercept)        38.833          5.090      7.630    3.23e-05    ***

x1                   -14.500          3.024     -4.795    0.000980   ***

x2                     -9.875          1.852     -5.333    0.000473   ***

---

Signif. codes:   0    ‘***’    0.001    ‘**’    0.01    ‘*’    0.05    ‘.’    0.1    ‘ ’    1

​

Residual standard error:    3.024    on    9    degrees   of    freedom

Multiple R-squared:    0.7633,    Adjusted R-squared:    0.7107

F-statistic:    14.51    on    2    and    9    DF,    p-value:    0.001527

bottom of page