Simulation and Matrices

Simulation

Why Simulate?

Simulation

Simulation

Simulation

Simulation

x <- rnorm(10, 0, 1)
x
##  [1] -0.0410621  1.7128294 -1.1591722 -0.9578240 -0.3778651  0.1273887
##  [7]  0.8001582  0.2627204 -0.7230348 -0.2266853
x <- rnorm(10)
x
##  [1]  0.4449222 -2.0122477 -0.1062258  0.7782035  0.4791456  1.2956687
##  [7]  1.4761441 -0.8072108  0.2729874  0.6531121

Setting the random number seed

set.seed(1)
x <- rnorm(10, 0, 1)
x
##  [1] -0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 -0.8204684
##  [7]  0.4874291  0.7383247  0.5757814 -0.3053884
set.seed(1)
x <- rnorm(10, 0, 1)
x ## Same as before
##  [1] -0.6264538  0.1836433 -0.8356286  1.5952808  0.3295078 -0.8204684
##  [7]  0.4874291  0.7383247  0.5757814 -0.3053884

Simulation

set.seed(1)
y <- rnorm(10, 20, 2)
y 
##  [1] 18.74709 20.36729 18.32874 23.19056 20.65902 18.35906 20.97486 21.47665
##  [9] 21.15156 19.38922
summary(y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   18.33   18.91   20.51   20.26   21.11   23.19

Simulation - Poisson Distribution

set.seed(1)
y <- rpois(10, 1)    ## Counts with a mean (lambda) of 1
y 
##  [1] 0 1 1 2 0 2 3 1 1 0
y <- rpois(10, 20)    ## Counts with a mean of 20
y 
##  [1] 16 18 19 30 23 16 17 18 18 21

Simulation - Binomial Distribution

set.seed(1)
y <- rbinom(10, 4, 0.5)    ## n=4 and p=0.5
y 
##  [1] 1 2 2 3 1 3 4 2 2 0
mean(y) # n*p
## [1] 2
x <- rbinom(n = 100, size = 5, prob = 0.5)
mean(x)
## [1] 2.49

Simulation - Exponential Distribution

data1 <- rexp(10, rate = 1)
mean(data1)
## [1] 1.669803
data2 <- rexp(100, rate = 1)
mean(data2)
## [1] 0.9870225
data3 <- rexp(1000, rate = 1)
mean(data3)
## [1] 1.026968
var(data3)
## [1] 1.014509

Simulating a Linear Model

set.seed(1)
x <- rnorm(100, 0, 1) ## Simulate predictor variable

e <- rnorm(100, 0, 2)  ## Simulate the error term  

y <- 0.5 + 2 * x + e    ## Compute the outcome via the model 
summary(y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -5.4816 -1.1493  0.7582  0.6422  2.3404  6.1534

Simulating a Linear Model

set.seed(1)
y <- 0.5 + 2 * x + e    
plot (x, y) 

Simulating a Linear Model

model <- lm(y~x)
summary(model) 
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.754 -1.228 -0.279  1.079  4.692 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   0.4246     0.1940   2.189    0.031 *  
## x             1.9979     0.2155   9.273 4.58e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.926 on 98 degrees of freedom
## Multiple R-squared:  0.4674, Adjusted R-squared:  0.4619 
## F-statistic: 85.99 on 1 and 98 DF,  p-value: 4.583e-15

Simulating a Linear Model

set.seed(1)
model <- lm(y~x)
par(mfrow = c(2, 2))

plot(model) 

Simulating Independent samples

set.seed(1)
scores <- rnorm(20, 10, 2)
groups <- rbinom(20, 1, 0.5)
groups 
##  [1] 1 1 1 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 1 0
boxplot(scores ~ groups)

Simulating Independent samples

shapiro.test(scores)
## 
##  Shapiro-Wilk normality test
## 
## data:  scores
## W = 0.95327, p-value = 0.4195

Simulating Independent samples

t.test(scores ~ groups, var.equal = TRUE)
## 
##  Two Sample t-test
## 
## data:  scores by groups
## t = -0.074459, df = 18, p-value = 0.9415
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -1.834543  1.708957
## sample estimates:
## mean in group 0 mean in group 1 
##        10.34651        10.40930

Simulating Independent samples

t.test(scores ~ groups, var.equal = FALSE)
## 
##  Welch Two Sample t-test
## 
## data:  scores by groups
## t = -0.071915, df = 14.06, p-value = 0.9437
## alternative hypothesis: true difference in means between group 0 and group 1 is not equal to 0
## 95 percent confidence interval:
##  -1.934786  1.809200
## sample estimates:
## mean in group 0 mean in group 1 
##        10.34651        10.40930

In-class Exercise

Matrices

How to Create a Matrix

# Only nrow or ncol need to be specified 
A <- matrix(1:12, nrow=3, byrow=F)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
B <- matrix(letters[1:12], nrow=3, byrow=T)
B
##      [,1] [,2] [,3] [,4]
## [1,] "a"  "b"  "c"  "d" 
## [2,] "e"  "f"  "g"  "h" 
## [3,] "i"  "j"  "k"  "l"

Stacking Columns or Rows Using cbind() and rbind()

# Another approach  
A <- cbind(1:3,4:6,7:9,10:12)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
B <- rbind(letters[1:4],letters[5:8],letters[9:12])
B
##      [,1] [,2] [,3] [,4]
## [1,] "a"  "b"  "c"  "d" 
## [2,] "e"  "f"  "g"  "h" 
## [3,] "i"  "j"  "k"  "l"

Naming Rows and Columns

names(A)
## NULL
rownames(A) <- c("row1","row2","row3")
A
##      [,1] [,2] [,3] [,4]
## row1    1    4    7   10
## row2    2    5    8   11
## row3    3    6    9   12
colnames(A) <- c("col1","col2","col3","col4")
A
##      col1 col2 col3 col4
## row1    1    4    7   10
## row2    2    5    8   11
## row3    3    6    9   12

Extracting Matrix Values by Index

A
##      col1 col2 col3 col4
## row1    1    4    7   10
## row2    2    5    8   11
## row3    3    6    9   12
dim(A)
## [1] 3 4
A[1:2,3:4]
##      col3 col4
## row1    7   10
## row2    8   11

Matrix Arithmetic

# 
Ar <- matrix(12:1, ncol=4)
Ar
##      [,1] [,2] [,3] [,4]
## [1,]   12    9    6    3
## [2,]   11    8    5    2
## [3,]   10    7    4    1
A+Ar
##      col1 col2 col3 col4
## row1   13   13   13   13
## row2   13   13   13   13
## row3   13   13   13   13

Matrix/Vector Arithmetic

A
##      col1 col2 col3 col4
## row1    1    4    7   10
## row2    2    5    8   11
## row3    3    6    9   12
A+1:3
##      col1 col2 col3 col4
## row1    2    5    8   11
## row2    4    7   10   13
## row3    6    9   12   15
A+(1:3)
##      col1 col2 col3 col4
## row1    2    5    8   11
## row2    4    7   10   13
## row3    6    9   12   15

Matrix/Vector Arithmetic

# Vectors are expanded by column to a conforming matrix
A
##      col1 col2 col3 col4
## row1    1    4    7   10
## row2    2    5    8   11
## row3    3    6    9   12
A+1:4
##      col1 col2 col3 col4
## row1    2    8   10   12
## row2    4    6   12   14
## row3    6    8   10   16

Matrix Multiply (Linear Algebra)

C <- matrix(1:4, nrow=2, ncol=2)
C
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
D <- matrix(6:1, nrow=2, ncol=3)
D
##      [,1] [,2] [,3]
## [1,]    6    4    2
## [2,]    5    3    1
C %*% D # Matrix can be multiplied using the command %*%
##      [,1] [,2] [,3]
## [1,]   21   13    5
## [2,]   32   20    8

Matrix Vector Multiply (Linear Algebra)

C <- matrix(1:4, nrow=2, ncol=2)
C
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
d <- c(2, 3)
d
## [1] 2 3
C %*% d
##      [,1]
## [1,]   11
## [2,]   16

Inverting a Square Matrix

G <- matrix(1:4, ncol=2)
G
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
solve(G)
##      [,1] [,2]
## [1,]   -2  1.5
## [2,]    1 -0.5

Inverting a Square Matrix

# Verifying answer 
solve(G)%*%G
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1

How to modify a matrix in R?

x <- matrix(1:9, nrow=3, byrow=F)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
# Replace x[2,2] by 10 
x[2,2] <- 10
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2   10    8
## [3,]    3    6    9
# Replace x[3,2] by 24 
x[3,2] <- 24
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2   10    8
## [3,]    3   24    9

How to modify a matrix in R?

x <- matrix(1:9, nrow=3, byrow=F)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
# Modify elements less than 5
x[x<5] <- 0
x
##      [,1] [,2] [,3]
## [1,]    0    0    7
## [2,]    0    5    8
## [3,]    0    6    9

Transpose a Matrix

x <- matrix(1:9, nrow=3, byrow=F)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
t(x)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Filling in a Matrix

my_results <- matrix(NA, nrow = 3, ncol = 3)
my_results
##      [,1] [,2] [,3]
## [1,]   NA   NA   NA
## [2,]   NA   NA   NA
## [3,]   NA   NA   NA

Filling in a Matrix

my_results[2, ] <- c(2, 4, 3)
my_results
##      [,1] [,2] [,3]
## [1,]   NA   NA   NA
## [2,]    2    4    3
## [3,]   NA   NA   NA

Filling in a Matrix

my_results[c(1, 3), ] <- 7
my_results
##      [,1] [,2] [,3]
## [1,]    7    7    7
## [2,]    2    4    3
## [3,]    7    7    7

Filling in a Matrix

my_results[c(1, 2, 3), ] <- 7
my_results
##      [,1] [,2] [,3]
## [1,]    7    7    7
## [2,]    7    7    7
## [3,]    7    7    7

Filling in a Matrix

my_results[1:3, ] <- 7
my_results
##      [,1] [,2] [,3]
## [1,]    7    7    7
## [2,]    7    7    7
## [3,]    7    7    7

Matrix Entry Types

rbind(c(1, 2, 3), c("a", "b", "c"))
##      [,1] [,2] [,3]
## [1,] "1"  "2"  "3" 
## [2,] "a"  "b"  "c"
rbind(c(1, 2, 3), c("a", "b", "c"), c("X", "Y", "Z"))
##      [,1] [,2] [,3]
## [1,] "1"  "2"  "3" 
## [2,] "a"  "b"  "c" 
## [3,] "X"  "Y"  "Z"

Common Matrix Operations in R

# Division
M1<- matrix(1:15, nrow=5)

M2<- matrix(2:16, nrow=5)

M1/M2
##           [,1]      [,2]      [,3]
## [1,] 0.5000000 0.8571429 0.9166667
## [2,] 0.6666667 0.8750000 0.9230769
## [3,] 0.7500000 0.8888889 0.9285714
## [4,] 0.8000000 0.9000000 0.9333333
## [5,] 0.8333333 0.9090909 0.9375000

Common Matrix Operations in R

M1<- matrix(1:15, nrow=5)
M1
##      [,1] [,2] [,3]
## [1,]    1    6   11
## [2,]    2    7   12
## [3,]    3    8   13
## [4,]    4    9   14
## [5,]    5   10   15
# Sum of rows in Matrix
rowSums(M1)
## [1] 18 21 24 27 30
# Sum of columns in Matrix
colSums(M1)
## [1] 15 40 65

Common Matrix Operations in R

M1<- matrix(1:15, nrow=5)
M1
##      [,1] [,2] [,3]
## [1,]    1    6   11
## [2,]    2    7   12
## [3,]    3    8   13
## [4,]    4    9   14
## [5,]    5   10   15
# Mean of rows in Matrix
rowMeans(M1)
## [1]  6  7  8  9 10
# Mean of columns in Matrix
colMeans(M1)
## [1]  3  8 13

Simple Linear Regression in Matrix Form

Example 1: Using R to Obtain the following components:

x <- c(2.5, 4.5, 5, 8.2, 9.3)
y <- c(-8, 16, 40, 115, 122)
model <- lm(y ~ x)
summary(model) 
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##       1       2       3       4       5 
##   5.672 -11.900   1.707  10.193  -5.672 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  -65.636     11.715  -5.603  0.01123 * 
## x             20.786      1.829  11.368  0.00146 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.21 on 3 degrees of freedom
## Multiple R-squared:  0.9773, Adjusted R-squared:  0.9697 
## F-statistic: 129.2 on 1 and 3 DF,  p-value: 0.001461

Example 1: Using R to Obtain SSE and MSE

summary(model)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##       1       2       3       4       5 
##   5.672 -11.900   1.707  10.193  -5.672 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  -65.636     11.715  -5.603  0.01123 * 
## x             20.786      1.829  11.368  0.00146 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.21 on 3 degrees of freedom
## Multiple R-squared:  0.9773, Adjusted R-squared:  0.9697 
## F-statistic: 129.2 on 1 and 3 DF,  p-value: 0.001461
SSE <- sum(model$residuals^2)
SSE
## [1] 312.7489
MSE <- sum(model$residuals^2)/3 # (n-2 = 5-2 =3)
MSE
## [1] 104.2496

Example 1: Using R to make Predictions and obtain Confidence Intervals

confint(model)
##                 2.5 %    97.5 %
## (Intercept) -102.9177 -28.35429
## x             14.9666  26.60492
confint(model, level = 0.90)
##                   5 %      95 %
## (Intercept) -93.20515 -38.06682
## x            16.48259  25.08893
predict(model, newdata = data.frame(x = 8.2))
##        1 
## 104.8072
predict(model, newdata = data.frame(x = 8.2), interval="confidence")
##        fit      lwr      upr
## 1 104.8072 85.05122 124.5633

Example 2: Use Matrix based OLS approach to fit a simple regression model for the following data.

X <- matrix(c(rep(1, 5), 2.5, 4.5, 5, 8.2, 9.3), nrow = 5)
X
##      [,1] [,2]
## [1,]    1  2.5
## [2,]    1  4.5
## [3,]    1  5.0
## [4,]    1  8.2
## [5,]    1  9.3
Y <- matrix(c(-8, 16, 40, 115, 122), nrow = 5)
Y
##      [,1]
## [1,]   -8
## [2,]   16
## [3,]   40
## [4,]  115
## [5,]  122

Example 2: Use Matrix based OLS approach to obtain Regression Coefficients

X <- matrix(c(rep(1, 5), 2.5, 4.5, 5, 8.2, 9.3), nrow = 5)
Y <- matrix(c(-8, 16, 40, 115, 122), nrow = 5)
betas <- solve(t(X) %*% X) %*% t(X) %*% Y
betas
##           [,1]
## [1,] -65.63598
## [2,]  20.78576

Example 2: Model Fit

SSE <- sum(model$residuals^2)
SSE
## [1] 312.7489
SST <- sum((Y - mean(Y))^2)
SST
## [1] 13784
R_square <- 1 - (SSE/SST)
R_square
## [1] 0.9773107
n <- 5 
R_square_adj <- 1 - ((n - 1) %*% (1 - R_square) %*% (1/(n - 2)))
R_square_adj
##           [,1]
## [1,] 0.9697476

Example 2: Computing Sum of Squares using Matrix

## Compute SSE
SSE <- t(Y) %*% Y - t(betas) %*% t(X) %*% Y 
SSE
##          [,1]
## [1,] 312.7489
## Compute SST
mc_y <- y - mean(y)
mc_y
## [1] -65 -41 -17  58  65
SST <- t(mc_y) %*% mc_y 
SST
##       [,1]
## [1,] 13784
## Compute SSR
SSR<- t(mc_y) %*% X %*% solve(t(X) %*% X) %*% t(X) %*% mc_y
SSR
##          [,1]
## [1,] 13471.25

Example 2: Computing Mean Squares using Matrix

n <- 5

## Compute regression mean square (MSR)
MSR <- SSR / (1) 
MSR
##          [,1]
## [1,] 13471.25
## Compute mean square error (MSE)
MSE <- SSE / ( n - 2) 
MSE
##          [,1]
## [1,] 104.2496
## Compute Test Statistic (F)
F <- MSR/MSE
F
##          [,1]
## [1,] 129.2211

Example 3: Example of simple linear regression in matrix form

y <- c(73,50,128,170,87,108,135,69,148,132)
x <- c(30,20,60,80,40,50,60,30,70,60)

model_1 <- lm(y ~ x)
summary(model_1) 
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
##   -3.0   -2.0   -0.5    1.5    5.0 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 10.00000    2.50294   3.995  0.00398 ** 
## x            2.00000    0.04697  42.583 1.02e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.739 on 8 degrees of freedom
## Multiple R-squared:  0.9956,    Adjusted R-squared:  0.9951 
## F-statistic:  1813 on 1 and 8 DF,  p-value: 1.02e-10

Example 3: Computing Regression Coefficients

y <- matrix(c(73,50,128,170,87,108,135,69,148,132), nrow = 10)
x <- matrix(c(rep(1, 10), 30,20,60,80,40,50,60,30,70,60), nrow = 10)


betas <- solve(t(x) %*% x) %*% t(x) %*% y
betas
##      [,1]
## [1,]   10
## [2,]    2

Example 3: Computing Coefficients of Determination

confint(model_1)
##                2.5 %    97.5 %
## (Intercept) 4.228211 15.771789
## x           1.891694  2.108306
SSE <- sum(model_1$residuals^2)
SSE
## [1] 60
SST <- sum((Y - mean(Y))^2)
SST
## [1] 13784
R_square <- 1 - (SSE/SST)
R_square
## [1] 0.9956471
n <- 10
R_square_adj <- 1 - ((n - 1) %*% (1 - R_square) %*% (1/(n - 2)))
R_square_adj
##          [,1]
## [1,] 0.995103

Example 3: Computing Residual Standard Error

SSE <- sum(model_1$residuals^2)
SSE
## [1] 60
n <- 10
MSE <- SSE / (n - 2) 
MSE 
## [1] 7.5
RSE <- sqrt(MSE) # Residual Standard Error
RSE
## [1] 2.738613

Example 3: Computing Sum of Squares using Matrix

## Compute SSE
SSE <- t(y) %*% y - t(betas) %*% t(x) %*% y 
SSE
##      [,1]
## [1,]   60
## Compute SST
mc_y <- y - mean(y)
mc_y
##       [,1]
##  [1,]  -37
##  [2,]  -60
##  [3,]   18
##  [4,]   60
##  [5,]  -23
##  [6,]   -2
##  [7,]   25
##  [8,]  -41
##  [9,]   38
## [10,]   22
SST <- t(mc_y) %*% mc_y 
SST
##       [,1]
## [1,] 13660
## Compute SSR
SSR<- t(mc_y) %*% x %*% solve(t(x) %*% x) %*% t(x) %*% mc_y
SSR
##       [,1]
## [1,] 13600

Example 3: Computing Mean Squares using Matrix

n <- 10

## Compute regression mean square (MSR)
MSR <- SSR / (1) 
MSR
##       [,1]
## [1,] 13600
## Compute mean square error (MSE)
MSE <- SSE / ( n - 2) 
MSE
##      [,1]
## [1,]  7.5
## Compute Test Statistic (F)
F <- MSR/MSE
F
##          [,1]
## [1,] 1813.333

Example 3: Computing variance-covariance matrix

Cov <-  solve(t(x) %*% x)
Cov
##             [,1]          [,2]
## [1,]  0.83529412 -0.0147058824
## [2,] -0.01470588  0.0002941176
V_b0 <- MSE  %*% Cov[1, 1]
V_b0
##          [,1]
## [1,] 6.264706
V_b1 <- MSE  %*% Cov[2, 2]
V_b1
##             [,1]
## [1,] 0.002205882

Example 3: Computing Confidence Intervals

n <- 10
t.val <- qt(0.975, n - 2)
t.val
## [1] 2.306004
b0 <- betas[1]
b1 <- betas[2]


## CI for the intercept 
b0 - t.val %*% sqrt (V_b0)
##          [,1]
## [1,] 4.228211
b0 + t.val %*% sqrt (V_b0)
##          [,1]
## [1,] 15.77179
## CI for the slope
b1 - t.val %*% sqrt (V_b1)
##          [,1]
## [1,] 1.891694
b1 + t.val %*% sqrt (V_b1)
##          [,1]
## [1,] 2.108306

Example 4: Matrix Approach to Multiple Linear Regression

## Dataset 

df <- structure(list(x1 = c(68.5, 45.2, 91.3, 47.8, 46.9, 66.1, 49.5, 
52, 48.9, 38.4, 87.9, 72.8, 88.4, 42.9, 52.5, 85.7, 41.3, 51.7, 
89.6, 82.7, 52.3), x2 = c(16.7, 16.8, 18.2, 16.3, 17.3, 18.2, 
15.9, 17.2, 16.6, 16, 18.3, 17.1, 17.4, 15.8, 17.8, 18.4, 16.5, 
16.3, 18.1, 19.1, 16), y = c(174.4, 164.4, 244.2, 154.6, 181.6, 
207.5, 152.8, 163.2, 145.4, 137.2, 241.9, 191.1, 232, 145.3, 
161.1, 209.7, 146.4, 144, 232.6, 224.1, 166.5)), .Names = c("x1", 
"x2", "y"), class = "data.frame", row.names = c(NA, -21L))

head(df)
##     x1   x2     y
## 1 68.5 16.7 174.4
## 2 45.2 16.8 164.4
## 3 91.3 18.2 244.2
## 4 47.8 16.3 154.6
## 5 46.9 17.3 181.6
## 6 66.1 18.2 207.5

Example 4: Using R to obtain results

fit <- lm(y ~ x1 + x2, data = df)
summary(fit)
## 
## Call:
## lm(formula = y ~ x1 + x2, data = df)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -18.4239  -6.2161   0.7449   9.4356  20.2151 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -68.8571    60.0170  -1.147   0.2663    
## x1            1.4546     0.2118   6.868    2e-06 ***
## x2            9.3655     4.0640   2.305   0.0333 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.01 on 18 degrees of freedom
## Multiple R-squared:  0.9167, Adjusted R-squared:  0.9075 
## F-statistic:  99.1 on 2 and 18 DF,  p-value: 1.921e-10
confint(fit)
##                    2.5 %    97.5 %
## (Intercept) -194.9480130 57.233867
## x1             1.0096226  1.899497
## x2             0.8274411 17.903560

Example 4: Obtaining the Design Matrix

X <- model.matrix(fit)  
t(X) 
##                1    2    3    4    5    6    7    8    9   10   11   12   13
## (Intercept)  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
## x1          68.5 45.2 91.3 47.8 46.9 66.1 49.5 52.0 48.9 38.4 87.9 72.8 88.4
## x2          16.7 16.8 18.2 16.3 17.3 18.2 15.9 17.2 16.6 16.0 18.3 17.1 17.4
##               14   15   16   17   18   19   20   21
## (Intercept)  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
## x1          42.9 52.5 85.7 41.3 51.7 89.6 82.7 52.3
## x2          15.8 17.8 18.4 16.5 16.3 18.1 19.1 16.0
## attr(,"assign")
## [1] 0 1 2
Y <- model.frame(fit)[1]
t(Y)
##       1     2     3     4     5     6     7     8     9    10    11    12  13
## y 174.4 164.4 244.2 154.6 181.6 207.5 152.8 163.2 145.4 137.2 241.9 191.1 232
##      14    15    16    17  18    19    20    21
## y 145.3 161.1 209.7 146.4 144 232.6 224.1 166.5

Example 4: Obtaining Regression Coefficients from the model

beta <- matrix(coef(fit))
beta
##           [,1]
## [1,] -68.85707
## [2,]   1.45456
## [3,]   9.36550

Example 4: Obtaining fitted values from the model

cbind (X %*% beta, fitted(fit))
##        [,1]     [,2]
## 1  187.1841 187.1841
## 2  154.2294 154.2294
## 3  234.3963 234.3963
## 4  153.3285 153.3285
## 5  161.3849 161.3849
## 6  197.7414 197.7414
## 7  152.0551 152.0551
## 8  167.8666 167.8666
## 9  157.7382 157.7382
## 10 136.8460 136.8460
## 11 230.3874 230.3874
## 12 197.1849 197.1849
## 13 222.6857 222.6857
## 14 141.5184 141.5184
## 15 174.2132 174.2132
## 16 228.1239 228.1239
## 17 145.7470 145.7470
## 18 159.0013 159.0013
## 19 230.9870 230.9870
## 20 230.3161 230.3161
## 21 157.0644 157.0644

Example 4: Obtaining fitted values from the model

cbind (X %*% beta, fitted(fit))
##        [,1]     [,2]
## 1  187.1841 187.1841
## 2  154.2294 154.2294
## 3  234.3963 234.3963
## 4  153.3285 153.3285
## 5  161.3849 161.3849
## 6  197.7414 197.7414
## 7  152.0551 152.0551
## 8  167.8666 167.8666
## 9  157.7382 157.7382
## 10 136.8460 136.8460
## 11 230.3874 230.3874
## 12 197.1849 197.1849
## 13 222.6857 222.6857
## 14 141.5184 141.5184
## 15 174.2132 174.2132
## 16 228.1239 228.1239
## 17 145.7470 145.7470
## 18 159.0013 159.0013
## 19 230.9870 230.9870
## 20 230.3161 230.3161
## 21 157.0644 157.0644