[R] lm함수를 통한 선형 회귀, 분할 정복을 통한 선형 회귀

2023. 10. 21. 18:29R, 빅데이터 분석 실험

1. lm 함수로 구하기

setwd('/Users/bagjeong-yong/desktop/bigdataA/data')
data1 <-  read.csv('data1.csv',header=TRUE)
data2 <-  read.csv('data2.csv',header=TRUE)
data3 <-  read.csv('data3.csv',header=TRUE)
data<-rbind(data1,data2,data3)
y =as.numeric(data[,1])
n = length(y)
x = as.matrix(cbind(rep(1,n),data[,2:5]))
lm(y~x)

해당 결과를 확인 할 수 있다.

 

A = matrix(0,5,5)
B = matrix(0,5,1)
setwd('/Users/bagjeong-yong/desktop/bigdataA/data')
data1 <-  read.csv('data1.csv',header=TRUE)
y =as.numeric(data1[,1])
n = length(y)
x = as.matrix(cbind(rep(1,n),data1[,2:5]))
A = A + t(x) %*% x
B = B + t(x) %*% y

data2 <-  read.csv('data2.csv',header=TRUE)
y = as.numeric(data2[,1])
n = length(y)
x = as.matrix(cbind(rep(1,n),data2[,2:5]))
A = A + t(x) %*% x
B = B + t(x) %*% y

data3 <-  read.csv('data3.csv',header=TRUE)
y =as.numeric(data3[,1])
n = length(y)
x = as.matrix(cbind(rep(1,n),data3[,2:5]))
A = A + t(x) %*% x
B = B + t(x) %*% y

r = solve(A) %*% B
r

값이 동일 함을 확인 할 수 있다.