library(ISLR)
A <- as.matrix(mtcars[, c("wt", "disp", "hp")])
B <- mtcars$mpg
x <- solve(t(A) %*% A, t(A) %*% B)
print(x)
## [,1]
## wt 12.06390563
## disp -0.11686153
## hp 0.04382227
prediksi <- A %*% x
hasil <- cbind(prediksi, B)
colnames(hasil) <- c("Prediksi", "Mpg")
print(hasil)
## Prediksi Mpg
## Mazda RX4 17.730038 21.0
## Mazda RX4 Wag 20.806333 21.0
## Datsun 710 19.442687 22.8
## Hornet 4 Drive 13.455631 21.4
## Hornet Sportabout 7.098581 18.7
## Valiant 20.048607 18.1
## Duster 360 11.734448 14.3
## Merc 240D 24.057253 24.4
## Merc 230 25.710315 22.8
## Merc 280 27.303982 19.2
## Merc 280C 27.303982 17.8
## Merc 450SE 24.757694 16.4
## Merc 450SL 20.655966 17.3
## Merc 450SLC 21.259162 15.2
## Cadillac Fleetwood 17.160427 10.4
## Lincoln Continental 21.100108 10.4
## Chrysler Imperial 23.141624 14.7
## Fiat 128 20.235860 32.4
## Honda Civic 12.915548 30.4
## Toyota Corolla 16.676860 33.9
## Toyota Corona 19.953218 21.5
## Dodge Challenger 11.876321 15.5
## AMC Javelin 12.486951 15.2
## Camaro Z28 16.160318 13.3
## Pontiac Firebird 7.310002 19.2
## Fiat X1-9 17.003866 27.3
## Porsche 914-2 15.746142 26.0
## Lotus Europa 12.091074 30.4
## Ford Pantera L 8.793263 15.8
## Ferrari Dino 24.140994 19.7
## Maserati Bora 22.573283 15.0
## Volvo 142E 24.174040 21.4
plot(mtcars$wt, mtcars$mpg, xlab = "Weight", ylab = "Mpg", main = "Hubungan antara Weight dan Mpg")
abline(lm(mtcars$mpg ~ mtcars$wt), col = "green")

plot(mtcars$disp, mtcars$mpg, xlab = "Displacement", ylab = "Mpg", main = "Hubungan antara Displacement dan Mpg")
abline(lm(mtcars$mpg ~ mtcars$disp), col = "yellow")

plot(mtcars$hp, mtcars$mpg, xlab = "Horsepower", ylab = "Mpg", main = "Hubungan antara Horsepower dan Mpg")
abline(lm(mtcars$mpg ~ mtcars$hp), col = "blue")
