Anda di halaman 1dari 36

Probabilitas dan Statistik

http://www.unhas.ac.id/amil/S1TIF/probstat2019/
L3
Regresi Linier
Amil Ahmad Ilham
Hubungan antarvariabel

Terhubung Tak Terhubung


Hubungan antarvariabel
Terhubung Linier Terhubung Non-linier

Terhubung Non-linier
Hubungan antarvariabel
Linier, terhubung kuat Linier, terhubung lemah
Regresi Linier

Linier, kuat Linier, Lemah Non-Linier


Regresi Linier - Residual
Linier, Kuat Linier, Lemah Non-Linier

Residuals

Residuals

Residuals
acak acak - tersebar berpola - tersebar
Koefisien korelasi
• Mengukur korelasi antara dua variabel
• Menunjukkan kekuatan korelasi

Pearson coefficient correlation:

r
 ( X  X )(Y  Y )
i i

 ( X  X )  (Y  Y )
i
2
i
2
koefisien korelasi
Apa itu regresi? Regresi adalah proses untuk memprediksi
nilai kontinu variable terikat

X: variable bebas Y: variable terikat


Ukuran Silinder Konsumsi Emisi CO2
Mesin BBM
0 2.0 4 8.5 196
1 2.4 4 9.6 221
2 1.5 4 5.9 136
3 3.5 6 11.1 255

Nilai kontinu
4 3.5 6 10.6 244
5 3.5 6 10.0 230
6 3.5 6 10.1 232
7 3.7 6 11.1 255
8 3.7 6 11.6 267
9 2.4 4 9.2 ?

Nilai kategoris atau kontinu 9


Apa itu model regresi?

Data historis mobil:


jumlah silinder, ukuran mesin,
konsumsi BBM dan CO2

Training

Model
Jenis mobil baru Estimasi
Emisi CO2
10
Jenis Model Regresi
• Simple Regression (regresi sederhana):
• Linier
• Non-linier
Prediksi EmisiCO2 vs Ukuran Mesin

• Multiple Regression (Regresi banyak variable):


• Linier
• Non-linier
Prediksi EmisiCO2 vs (Ukuran Mesin dan jumlah silinder)

11
Aplikasi Regresi
• Prediksi Nilai Penjualan per Tahun seseorang
• Berdasarkan umur, pendidikan dan pengalaman seorang sales
• Analisis kepuasan pelanggan
• Berdasarkan demografi dan faktor psikologis pelanggan
• Estimasi Harga Rumah
• Berdasarkan luas, jumlah kamar dll
• Gaji Karyawan
• Berdasarkan jenis pekerjaan, pendidikan, jenis kelamin, umur, pengalaman
kerja dll.
Berbagai Algoritma Regresi
• Regresi Ordinal
• Regresi Poisson
• Fast Forest Quantile
• Linier, Polinomial, Lasso, Stepwise, Ridge
• Regresi Linier Bayesian
• Neural Network
• Decision Forest
• Booseted Decision Tree
• KNN (K-nearest neighbors)

13
Regresi Linier Sederhana

14
Menggunakan Regresi Linier untuk memperkirakan nilai
kontinu
X: variable bebas Y: variable terikat
Ukuran Silinder Konsumsi Emisi CO2
Mesin BBM
0 2.0 4 8.5 196
1 2.4 4 9.6 221
2 1.5 4 5.9 136
3 3.5 6 11.1 255

Nilai kontinu
4 3.5 6 10.6 244
5 3.5 6 10.0 230
6 3.5 6 10.1 232
7 3.7 6 11.1 255
8 3.7 6 11.6 267
9 2.4 4 9.2 ?
15
Topologi Regresi Linier
• Regresi Linier Sederhana
• Prediksi Emisi CO2 vs Ukuran Mesin dari semua mobil

Variabel bebas(x1): Regresi Linier Prediksi (y):


Ukuran Mesin Sederhana Emisi CO2

• Regresi Linier Berganda (Multiple Linear Regression):


• Prediksi Emisi CO2 vs Ukuran Mesin dan Silinder semua mobil

Variabel bebas(x1, x2,…): Regresi Linier Prediksi (y):


Ukuran Mesin, Silinder, … Berganda Emisi CO2

16
Cara menerapkan regresi linier

Ukuran Silinder Konsumsi Emisi


Mesin BBM CO2
0 2.0 4 8.5 196
1 2.4 4 9.6 221

Emisi CO2
2 1.5 4 5.9 136
3 3.5 6 11.1 255
4 3.5 6 10.6 244
5 3.5 6 10.0 230
6 3.5 6 10.1 232
7 3.7 6 11.1 255
8 3.7 6 11.6 267
9 2.4 4 9.2 ? Ukuran Mesin
Cara menerapkan regresi linier
Plot Data Y vs X
Ukuran Silinder Konsumsi Emisi
Mesin BBM CO2
0 2.0 4 8.5 196
1 2.4 4 9.6 221 214

Y (Emisi CO2)
2 1.5 4 5.9 136
3 3.5 6 11.1 255
4 3.5 6 10.6 244
5 3.5 6 10.0 230
6 3.5 6 10.1 232
7 3.7 6 11.1 255
8 3.7 6 11.6 267
9 2.4 4 9.2 ? x1 (Ukuran Mesin)
2.4
Cara menerapkan regresi linier
Koefisien garis Plot Data Y vs X

Perpotongan sumbu y gradien

Y (Emisi CO2)
𝑦 = 𝜃0 + 𝜃1 𝑥1

Variabel prediksi Variabel bebas


tunggal
x1 (Ukuran Mesin)

Bagaimana mencari nilai 𝜃0 dan 𝜃1 sehingga garis paling mendekati data (best fit) ?
Cara mencari best fit ?
Plot Data Y vs X
Ambil nilai variable bebas 𝑥1 = 5.4
𝑦 = 250 (nilai Emisi CO2 menurut data)

Y (Emisi CO2)
𝑦 = 𝜃0 + 𝜃1 𝑥1 𝒚 = 𝟑𝟒𝟎

𝑦 = 340 adalah hasil prediksi untuk x1


𝒚 = 𝟐𝟓𝟎
Error = 𝑦 − 𝑦
= 250 − 340
= −90

Karena nilai error bisa positif atau negatif, 5.4

gunakan nilai kwardratnya. Best Fit adalah x1 (Ukuran Mesin)


mencari garis yang jumlah kwardrat error – nya paling kecil (minimal square error):
𝑛
1 2
𝑀𝑆𝐸 = 𝑦𝑖 − 𝑦𝑖 minimasi 𝜃0 , 𝜃1
𝑛
𝑖=1
Cara mencari best fit ?
Plot Data Y vs X

𝑛
1 2
𝑀𝑆𝐸 = 𝑦𝑖 − 𝑦𝑖

Y (Emisi CO2)
𝑛
𝑖=1

Pendekatan minimasi:
• Pendekatan matematis
• Pendekatan optimasi
x1 (Ukuran Mesin)

Distribusi Error
Estimasi parameter 𝜃0 dan 𝜃1 (𝒎𝒂𝒕𝒆𝒎𝒂𝒕𝒊𝒔)
𝑦 = 𝜃0 + 𝜃1 𝑥1
Ukuran Silinder Konsumsi Emisi
Mesin BBM CO2 𝑠
0 2.0 4 8.5 196 𝑖=1 𝑥𝑖 − 𝑥 𝑦𝑖 − 𝑦
𝜃1 = 𝑠 2
1 2.4 4 9.6 221
𝑖=1 𝑥𝑖 − 𝑥
2 1.5 4 5.9 136
3 3.5 6 11.1 255
4 3.5 𝑥1 6 10.6 244 𝑦
𝜃1 = 43.98
5 3.5 6 10.0 230
6 3.5 6 10.1 232
7 3.7 6 11.1 255 𝜃𝑜 = 𝑦 − 𝜃1 𝑥
8 3.7 6 11.6 267
9 2.4 4 9.2 ?
𝜃𝑜 = 92.8
Estimasi parameter 𝜃0 dan 𝜃1 (𝒐𝒑𝒕𝒊𝒎𝒂𝒔𝒊)

Ukuran Silinder Konsumsi Emisi


Mesin BBM CO2
𝑦 = 92.8 + 43.98 𝑥1
0 2.0 4 8.5 196
1 2.4 4 9.6 221
2 1.5 4 5.9 136
3 3.5 6 11.1 255
4 3.5 𝑥1 6 10.6 244 𝑦
5 3.5 6 10.0 230
6 3.5 6 10.1 232
7 3.7 6 11.1 255
8 3.7 6 11.6 267
9 2.4 4 9.2 ?
Prediksi dengan Regresi Linier

Ukuran Silinder Konsumsi Emisi


Mesin BBM CO2
𝑦 = 92.8 + 43.98 𝑥1
0 2.0 4 8.5 196
1 2.4 4 9.6 221
2 1.5 4 5.9 136
3 3.5 6 11.1 255
4 3.5 6 10.6 244
5 3.5 6 10.0 230
6 3.5 6 10.1 232
7 3.7 6 11.1 255
8 3.7 6 11.6 267
9 2.4 4 9.2 ? 198.352
Tutorial Simple Linear Regression
• Downloas file automobileEDA.csv di
www.unhas.ac.id/amil/S1TIF/probstat2019/
• Klik kanan file => Save Link As => Save as type: All Files
• Run Jupyter Notebook
Dataset
Contoh plot hubungan antara 'highway-mpg'
dan 'price’

Miles per gallon (mpg)


Koefisien korelasi
• 'highway-mpg' and 'price'
Cek koefisien korelasi
• 'engine-size' and 'price'
Cek koefisien korelasi
• 'Peak-rpm' and 'price'
Linear function

• a refers to the intercept of the regression line, in other words: the


value of Y when X is 0
• b refers to the slope of the regression line, in other words: the value
with which Y changes when X increases by 1 unit
Load the modules for linear regression
Linear function
• "highway-mpg" as the predictor variable and the "price" as the
response variable.
Linear function
• "highway-mpg" as the predictor variable and the "price" as the
response variable.
Model Linier
Plotting data dan model

Anda mungkin juga menyukai