launch <- read.csv("/cloud/project/Data/launch.txt")
#View(launch)
#variable.names(launch)
#names(launch)[names(launch) == "temp"] <- "temperature"
#names(launch)[names(launch) == "damage"] <- "distress_ct"
variable.names(launch)
## [1] "o_ring_ct" "distress_ct" "temperature" "pressure" "launch_id"
tìm một đường simple linear Regression sao cho b đạt giá trị nhỏ nhất, được xác định bằng công thức
b = Cov(x,y)/Var(x)
x: ở đây là temperature và
y: là biến distress_ct
b <- cov(launch$temperature, launch$distress_ct) /
var(launch$temperature)
b
## [1] -0.05746032
a <- mean(launch$distress_ct) - b * mean(launch$temperature)
a # có b rồi suy ra a từ công thức y = b + ax
## [1] 4.301587
plot(launch$temperature, launch$distress_ct)
Correlation là định nghĩa về mối liên hệ giữa hai đại lượng x và y trong y = b + a*x hay còn gọi là đại lượng Pearson’s correlation coeffient , đại lượng là giao động từ -1 đến 1 được xác định bằng công thức
P(x,y) = Corr(x,y)= Cov(x,y)/e(x)e(y)
trong đó: e là standand devitation sai số chuẩn, lệnh sd() trong R
Cov là covariance hiệp biến, hay hiệp phương sai ?? tìm hiểu thêm
Trong bảng tính bên dưới giá trị -0.7 cho thấy nếu tăng temperature (x) thì distress_ct (y) sẽ giảm tương ứng 0.7 vì có dấu âm, nếu dương thì ngược lại x tăng thì y tăng.
r <- cov(launch$temperature, launch$distress_ct) /
(sd(launch$temperature) * sd(launch$distress_ct))
r
## [1] -0.725671
cor(launch$temperature, launch$distress_ct)
## [1] -0.725671
Chuyển qua phần multiple Linear Regression
Phần này cũng đi tìm một giá trị b (bêta) sao cho giá trị này đạt nhỏ nhất, nhưng khác ở đây là gồm nhiều biến độc lập chứ không giống như simple linear chỉ gồm 1 biến y = b + ax , trong phần multiple thì nhiều hơn gồm y = + β0 + β1x1 + β2x2 + βixi + ε , được viết lại là : Y = X*β + ε X, Y, β là các ma trận (matrix), G
Giờ đây để tiện cho việc tính toán, chúng ta sẽ tính trên các ma trận
Chúng ta cũng đi tìm β sao cho β^ là nhỏ nhất
Viết công thức cho R
reg <- function(y, x) {
x <- as.matrix(x)
x <- cbind(Intercept = 1, x)
solve(t(x) %*% x) %*% t(x) %*% y
}
This function uses several R commands we have not used previously. First, since we will be using the function with sets of columns from a data frame, the as.matrix() function is used to coerce the data into matrix form. Next, the cbind() function is used to bind an additional column onto the x matrix; the command Intercept = 1 instructs R to name the new column Intercept and to fill the column with repeating 1 values. Finally, a number of matrix operations are performed on the x and y objects:
• solve() takes the inverse of a matrix nghịch đảo
• t() is used to transpose a matrix hoán vị
• %*% multiplies two matrices
str(launch)
## 'data.frame': 23 obs. of 5 variables:
## $ o_ring_ct : int 6 6 6 6 6 6 6 6 6 6 ...
## $ distress_ct: int 0 1 0 0 0 0 0 0 1 1 ...
## $ temperature: int 66 70 69 68 67 72 73 70 57 63 ...
## $ pressure : int 50 50 50 50 50 50 100 100 200 200 ...
## $ launch_id : int 1 2 3 4 5 6 7 8 9 10 ...
reg(y = launch$distress_ct, x = launch[3]) # launch[3] là biến hay cột số 3, một cách viết khác
## [,1]
## Intercept 4.30158730
## temperature -0.05746032
reg(y= launch$distress_ct, x= launch[3:5])
## [,1]
## Intercept 3.814247216
## temperature -0.055068768
## pressure 0.003428843
## launch_id -0.016734090