Pengaplikasian gausian linear pada dataset mtcars

pertama kita buat terlebih dahulu gausian linear nya :

library(matlib)
## Warning: package 'matlib' was built under R version 4.2.3
A <- matrix(c(0, -1, 1, 0, 1, 1, 0, 1, 3, -4, 2, 0, -1, 0, 4, -4), 4, 4)
b <- c(1, 1, 5, -2)
showEqn(A, b)
##  0*x1 + 1*x2 + 3*x3 - 1*x4  =   1 
## -1*x1 + 1*x2 - 4*x3 + 0*x4  =   1 
##  1*x1 + 0*x2 + 2*x3 + 4*x4  =   5 
##  0*x1 + 1*x2 + 0*x3 - 4*x4  =  -2
echelon(A, b, verbose=TRUE, fractions=TRUE)
## 
## Initial matrix:
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  0    1    3   -1    1  
## [2,] -1    1   -4    0    1  
## [3,]  1    0    2    4    5  
## [4,]  0    1    0   -4   -2  
## 
## row: 1 
## 
##  exchange rows 1 and 2 
##      [,1] [,2] [,3] [,4] [,5]
## [1,] -1    1   -4    0    1  
## [2,]  0    1    3   -1    1  
## [3,]  1    0    2    4    5  
## [4,]  0    1    0   -4   -2  
## 
##  multiply row 1 by -1 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1   -1    4    0   -1  
## [2,]  0    1    3   -1    1  
## [3,]  1    0    2    4    5  
## [4,]  0    1    0   -4   -2  
## 
##  subtract row 1 from row 3 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1   -1    4    0   -1  
## [2,]  0    1    3   -1    1  
## [3,]  0    1   -2    4    6  
## [4,]  0    1    0   -4   -2  
## 
## row: 2 
## 
##  multiply row 2 by 1 and add to row 1 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    7   -1    0  
## [2,]  0    1    3   -1    1  
## [3,]  0    1   -2    4    6  
## [4,]  0    1    0   -4   -2  
## 
##  subtract row 2 from row 3 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    7   -1    0  
## [2,]  0    1    3   -1    1  
## [3,]  0    0   -5    5    5  
## [4,]  0    1    0   -4   -2  
## 
##  subtract row 2 from row 4 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    7   -1    0  
## [2,]  0    1    3   -1    1  
## [3,]  0    0   -5    5    5  
## [4,]  0    0   -3   -3   -3  
## 
## row: 3 
## 
##  multiply row 3 by -1/5 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    7   -1    0  
## [2,]  0    1    3   -1    1  
## [3,]  0    0    1   -1   -1  
## [4,]  0    0   -3   -3   -3  
## 
##  multiply row 3 by 7 and subtract from row 1 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    0    6    7  
## [2,]  0    1    3   -1    1  
## [3,]  0    0    1   -1   -1  
## [4,]  0    0   -3   -3   -3  
## 
##  multiply row 3 by 3 and subtract from row 2 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    0    6    7  
## [2,]  0    1    0    2    4  
## [3,]  0    0    1   -1   -1  
## [4,]  0    0   -3   -3   -3  
## 
##  multiply row 3 by 3 and add to row 4 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    0    6    7  
## [2,]  0    1    0    2    4  
## [3,]  0    0    1   -1   -1  
## [4,]  0    0    0   -6   -6  
## 
## row: 4 
## 
##  multiply row 4 by -1/6 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    0    6    7  
## [2,]  0    1    0    2    4  
## [3,]  0    0    1   -1   -1  
## [4,]  0    0    0    1    1  
## 
##  multiply row 4 by 6 and subtract from row 1 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    0    0    1  
## [2,]  0    1    0    2    4  
## [3,]  0    0    1   -1   -1  
## [4,]  0    0    0    1    1  
## 
##  multiply row 4 by 2 and subtract from row 2 
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  1    0    0    0    1  
## [2,]  0    1    0    0    2  
## [3,]  0    0    1   -1   -1  
## [4,]  0    0    0    1    1  
## 
##  multiply row 4 by 1 and add to row 3 
##      [,1] [,2] [,3] [,4] [,5]
## [1,] 1    0    0    0    1   
## [2,] 0    1    0    0    2   
## [3,] 0    0    1    0    0   
## [4,] 0    0    0    1    1
Solve(A, b)
## x1        =  1 
##   x2      =  2 
##     x3    =  0 
##       x4  =  1
A <-matrix(c(1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,1),nrow=4,ncol=4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    0    0
## [2,]    0    0    1    1
## [3,]    1    0    1    0
## [4,]    0    1    0    1
b <-c(475,489,542,422)
Solve(A, b)
## x1     - 1*x4  =   53 
##   x2     + x4  =  422 
##     x3   + x4  =  489 
##             0  =    0

Lalu kita dapat membuat dataset dari “mtcars” dengan menjalankan perintah berikut :

data(mtcars)

Selanjutnya, kita dapat menentukan variabel dependen (y) dan variabel independen (x) yang akan digunakan dalam model. Untuk contoh ini, kita akan menggunakan variabel "mpg" sebagai y dan variabel "wt" sebagai x. Kita juga dapat melakukan plot scatterplot untuk memeriksa apakah terdapat korelasi antara variabel dependen dan independen. Berikut adalah kode untuk membuat scatterplot:

plot(mtcars$wt, mtcars$mpg, xlab = "height", ylab = "Miles per hours")

Selanjutnya, kita dapat membangun model linear sederhana dengan menggunakan fungsi "lm". Kita dapat menentukan variabel dependen dan independen dalam fungsi lm, seperti berikut:

model <- lm(mpg ~ wt, data = mtcars)

Setelah model dibangun, kita dapat mengevaluasi kualitas model dengan memeriksa koefisien determinasi (R-squared) dan plot residual. Berikut adalah kode untuk menampilkan R-squared dan membuat plot residual:

# menampilkan R-squared
summary(model)$r.squared
## [1] 0.7528328
# membuat plot residual
plot(model, which = 1)