Upload
others
View
50
Download
3
Embed Size (px)
Citation preview
Probabilitas dan Statistikhttp://www.unhas.ac.id/amil/S1TIF/probstat2019/
L3
Regresi Linier
Amil Ahmad Ilham
Hubungan antarvariabel
Terhubung Tak Terhubung
Terhubung Linier
Terhubung Non-linier
Terhubung Non-linier
Hubungan antarvariabel
Linier, terhubung kuat Linier, terhubung lemah
Hubungan antarvariabel
Linier, kuat Linier, Lemah Non-Linier
Regresi Linier
Linier, Kuat Linier, Lemah Non-Linier
Resi
dua
ls
Resi
dua
ls
Resi
dua
ls
Regresi Linier - Residual
acak acak - tersebar berpola - tersebar
Koefisien korelasi
22 )()(
))((
YYXX
YYXXr
ii
ii
• Mengukur korelasi antara dua variabel
• Menunjukkan kekuatan korelasi
Pearson coefficient correlation:
koefisien korelasi
Apa itu regresi?
UkuranMesin
Silinder KonsumsiBBM
Emisi CO2
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 ?
9
Regresi adalah proses untuk memprediksinilai kontinu variable terikat
Nila
i ko
nti
nu
X: variable bebas Y: variable terikat
Nilai kategoris atau kontinu
Apa itu model regresi?
10
Data historis mobil:jumlah silinder, ukuran mesin,
konsumsi BBM dan CO2
Model
Jenis mobil baru EstimasiEmisi CO2
Training
Jenis Model Regresi
• Simple Regression (regresi sederhana):• Linier
• Non-linier
• Multiple Regression (Regresi banyak variable):• Linier
• Non-linier
11
Prediksi EmisiCO2 vs Ukuran Mesin
Prediksi EmisiCO2 vs (Ukuran Mesin dan jumlah silinder)
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
UkuranMesin
Silinder KonsumsiBBM
Emisi CO2
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 ?
15
Nila
i ko
nti
nu
X: variable bebas Y: variable terikat
Menggunakan Regresi Linier untuk memperkirakan nilaikontinu
Topologi Regresi Linier
• Regresi Linier Sederhana• Prediksi Emisi CO2 vs Ukuran Mesin dari semua mobil
• Regresi Linier Berganda (Multiple Linear Regression):• Prediksi Emisi CO2 vs Ukuran Mesin dan Silinder semua mobil
16
Regresi LinierSederhana
Variabel bebas(x1): Ukuran Mesin
Prediksi (y): Emisi CO2
Regresi LinierBerganda
Variabel bebas(x1, x2,…): Ukuran Mesin, Silinder, …
Prediksi (y): Emisi CO2
Cara menerapkan regresi linier
UkuranMesin
Silinder KonsumsiBBM
EmisiCO2
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 ? Ukuran Mesin
Emis
iCO
2
Cara menerapkan regresi linier
UkuranMesin
Silinder KonsumsiBBM
EmisiCO2
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 ? x1 (Ukuran Mesin)
Y (E
mis
iCO
2)
2.4
214
Plot Data Y vs X
Cara menerapkan regresi linier
x1 (Ukuran Mesin)
Y (E
mis
iCO
2)
Plot Data Y vs X
𝑦 = 𝜃0 + 𝜃1 𝑥1
Variabel prediksi
Perpotongan sumbu y gradien
Koefisien garis
Variabel bebastunggal
Bagaimana mencari nilai 𝜃0 dan 𝜃1 sehingga garis paling mendekati data (best fit) ?
Cara mencari best fit ?
x1 (Ukuran Mesin)
Y (E
mis
iCO
2)
Plot Data Y vs XAmbil nilai variable bebas 𝑥1 = 5.4𝑦 = 250 (nilai Emisi CO2 menurut data)
𝑦 = 𝜃0 + 𝜃1 𝑥1 𝑦 = 340 adalah hasil prediksi untuk x1
Error = 𝑦 − 𝑦= 250 − 340= −90
Karena nilai error bisa positif atau negatif, gunakan nilai kwardratnya. Best Fit adalahmencari garis yang jumlah kwardrat error – nya paling kecil (minimal square error):
𝑀𝑆𝐸 =1
𝑛
𝑖=1
𝑛
𝑦𝑖 − 𝑦𝑖2
5.4
𝒚 = 𝟐𝟓𝟎
𝒚 = 𝟑𝟒𝟎
minimasi 𝜃0, 𝜃1
Cara mencari best fit ?
x1 (Ukuran Mesin)
Y (E
mis
iCO
2)
Plot Data Y vs X
Pendekatan minimasi:• Pendekatan matematis• Pendekatan optimasi
𝑀𝑆𝐸 =1
𝑛
𝑖=1
𝑛
𝑦𝑖 − 𝑦𝑖2
Distribusi Error
Estimasi parameter 𝜃0 dan 𝜃1 (𝒎𝒂𝒕𝒆𝒎𝒂𝒕𝒊𝒔)
UkuranMesin
Silinder KonsumsiBBM
EmisiCO2
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 ?
𝑥1 𝑦
𝑦 = 𝜃0 + 𝜃1 𝑥1
𝜃1 = 𝑖=1𝑠 𝑥𝑖 − 𝑥 𝑦𝑖 − 𝑦
𝑖=1𝑠 𝑥𝑖 − 𝑥 2
𝜃1 = 43.98
𝜃𝑜 = 𝑦 − 𝜃1 𝑥
𝜃𝑜 = 92.8
Estimasi parameter 𝜃0 dan 𝜃1 (𝒐𝒑𝒕𝒊𝒎𝒂𝒔𝒊)
UkuranMesin
Silinder KonsumsiBBM
EmisiCO2
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 ?
𝑥1 𝑦
𝑦 = 92.8 + 43.98 𝑥1
Prediksi dengan Regresi Linier
UkuranMesin
Silinder KonsumsiBBM
EmisiCO2
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 ?
𝑦 = 92.8 + 43.98 𝑥1
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