Algorithm, Mathematic, Programming, Python

Menghitung regresi linear berganda menggunakan bahasa pemrograman python

Analisis regresi linier berganda adalah hubungan secara linear antara dua atau lebih variabel independen (X1, X2,….Xn) dengan variabel dependen (Y). Analisis ini untuk mengetahui arah hubungan antara variabel independen dengan variabel dependen apakah masing-masing variabel independen berhubungan positif atau negatif dan untuk memprediksi nilai dari variabel dependen apabila nilai variabel independen mengalami kenaikan atau penurunan. Data yang digunakan biasanya berskala interval atau rasio.

Persamaan regresi linear berganda sebagai berikut:

Y’ = a + b1X1+ b2X2+…..+ bnXn

Keterangan:

Y’ = Variabel dependen (nilai yang diprediksikan)

X1 dan X= Variabel independen

a =  Konstanta (nilai Y’ apabila X1, X2…..Xn = 0)

b = Koefisien regresi (nilai peningkatan ataupun penurunan)


import math
import numpy as np

y_arr = [
	33, 36, 32, 36, 36,
	31, 38, 39, 33, 37
]

x1_arr = [
	34, 32, 31, 37, 36,
	32, 40, 40, 40, 37
]

x2_arr = [
	17, 18, 15, 17, 17,
	24, 12, 18, 14, 17
]

x3_arr = [
	9, 15,  9, 9, 9,
	9, 12, 10, 9, 7
]

n = 10

y_sum = 0

x1_sum = 0
x2_sum = 0
x3_sum = 0

x1y_sum = 0
x2y_sum = 0
x3y_sum = 0

x1x2_sum = 0
x1x3_sum = 0
x2x3_sum = 0

x1pow2_sum = 0
x2pow2_sum = 0
x3pow2_sum = 0

for x in range(0, n):
	# hitung y_sum
	y_sum += y_arr[x]
	# hitung x1_sum
	x1_sum += x1_arr[x]
	# hitung x2_sum
	x2_sum += x2_arr[x]
	# hitung x3_sum
	x3_sum += x3_arr[x]
	
	# hitung x1y_sum
	x1y = x1_arr[x] * y_arr[x]
	x1y_sum += x1y

	# hitung x2y_sum
	x2y = x2_arr[x] * y_arr[x]
	x2y_sum += x2y

	# hitung x3y_sum
	x3y = x3_arr[x] * y_arr[x]
	x3y_sum += x3y

	# hitung x1x2_sum
	x1x2 = x1_arr[x] * x2_arr[x]
	x1x2_sum += x1x2

	# hitung x1x3_sum
	x1x3 = x1_arr[x] * x3_arr[x]
	x1x3_sum += x1x3

	# hitung x2x3_sum
	x2x3 = x2_arr[x] * x3_arr[x]
	x2x3_sum += x2x3

	# hitung x1pow2_sum
	x1pow2 = math.pow(x1_arr[x], 2)
	x1pow2_sum += x1pow2

	# hitung x2pow2_sum
	x2pow2 = math.pow(x2_arr[x], 2)
	x2pow2_sum += x2pow2

	# hitung x3pow2_sum
	x3pow2 = math.pow(x3_arr[x], 2)
	x3pow2_sum += x3pow2

# regresi berganda
# y = a0 + a1x1 + a2x2 + a3x3

# diperoleh 4 persamaan regresi yang berbentuk
# sum y 	= a0n 		 + a1 (sum x1)   + a2(sum x2)    + a3 (sum x3)
# sum yx1 	= a0(sum x1) + a1 (sum x1^2) + a2 (sum x1x2) + a3 (sum x1x3)
# sum yx2 	= a0(sum x2) + a1 (sum x1x2) + a2 (sum x2^2) + a3 (sum x2x3)
# sum yx3 	= a0(sum x3) + a1 (sum x1x3) + a2 (sum x2x3) + a3 (sum x3^2)

# diperoleh 4 persamaan regresi yang berbentuk
# print("Pers 1: {}(a0) \t+ {}(a1) \t+ {}(a2) \t+ {}(a3) \t= {}".format(n	   , x1_sum 	, x2_sum 	, x3_sum 	 , y_sum))
# print("Pers 2: {}(a0) + {}(a1) + {}(a2) \t+ {}(a3) \t= {}"	.format(x1_sum , int(x1pow2_sum)	, x1x2_sum	, x1x3_sum 	 , x1y_sum))
# print("Pers 3: {}(a0) + {}(a1) \t+ {}(a2) \t+ {}(a3) \t= {}"	.format(x2_sum , x1x2_sum	, int(x2pow2_sum), x2x3_sum 	 , x2y_sum))
# print("Pers 4: {}(a0) \t+ {}(a1) \t+ {}(a2) \t+ {}(a3) \t= {}"  .format(x3_sum , x1x3_sum 	, x2x3_sum 	, int(x3pow2_sum) , x3y_sum))

b1 = y_sum
b2 = x1y_sum
b3 = x2y_sum
b4 = x3y_sum

ARR = [
	[n,x1_sum, x2_sum, x3_sum],
	[x1_sum, int(x1pow2_sum), x1x2_sum, x1x3_sum],
	[x2_sum, x1x2_sum, int(x2pow2_sum), x2x3_sum],
	[x3_sum, x1x3_sum, x2x3_sum, x3pow2_sum],
]

C1 = [
	[b1, x1_sum         , x2_sum, x3_sum],
	[b2, int(x1pow2_sum), x1x2_sum, x1x3_sum],
	[b3, x1x2_sum       , int(x2pow2_sum), x2x3_sum],
	[b4, x1x3_sum       , x2x3_sum, x3pow2_sum],
]

C2 = [
	[n     , b1, x2_sum         , x3_sum],
	[x1_sum, b2, x1x2_sum       , x1x3_sum],
	[x2_sum, b3, int(x2pow2_sum), x2x3_sum],
	[x3_sum, b4, x2x3_sum       , x3pow2_sum],
]

C3 = [
	[n     ,x1_sum          , b1, x3_sum],
	[x1_sum, int(x1pow2_sum), b2, x1x3_sum],
	[x2_sum, x1x2_sum       , b3, x2x3_sum],
	[x3_sum, x1x3_sum       , b4, x3pow2_sum],
]

C4 = [
	[n     ,x1_sum          , x2_sum         , b1],
	[x1_sum, int(x1pow2_sum), x1x2_sum       , b2],
	[x2_sum, x1x2_sum       , int(x2pow2_sum), b3],
	[x3_sum, x1x3_sum       , x2x3_sum       , b4],
]

# detA
detA = np.linalg.det(ARR)
# detC1
detC1 = np.linalg.det(C1)
# detC2
detC2 = np.linalg.det(C2)
# detC3
detC3 = np.linalg.det(C3)
# detC4
detC4 = np.linalg.det(C4)

# A0
a0 = detC1 / detA
# A1
a1 = detC2 / detA
# A2
a2 = detC3 / detA
# A3
a3 = detC4 / detA

# sehingga diperoleh persamaan
print "y = {} + {}(x1) + {}(x2) + {}(x3)".format(a0,a1,a2,a3)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s