tạo 1 function : chạy hồi quy bội, kiểm định khuyết tật, dự báo

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(lmtest)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
# Hàm function thực hiện phân tích kinh tế lượng
kinhteluong <- function(stock_data_path) {
stock_data <- readxl::read_excel(stock_data_path)

# chạy hồi quy bội
kqhoiquy <- lm(close ~ open + high + low, data = stock_data)

# kiểm định khuyết tật Breusch-Godfrey Test
kdkhuyetat <- bgtest(kqhoiquy)

# dự báo sử dụng mô hình arima
close_cd <- ts(stock_data$close, frequency = 7)
mohinharima <- auto.arima(close_cd)

# dự báo cho 7 ngày tiếp theo 
kddubao <- forecast(mohinharima, h = 7)

return(list(kqhoiquy = kqhoiquy,kdkhuyetat = kdkhuyetat,kddubao = kddubao))}

# gọi hàm để thực hiện phân tích kinh tế lượng với dữ liệu từ tệp "stock.xlsx"
kqkinhteluong <- kinhteluong("D:/stock.xlsx")

# xem kết quả hồi quy
summary(kqkinhteluong$kqhoiquy)
## 
## Call:
## lm(formula = close ~ open + high + low, data = stock_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -49.160  -7.384   2.124   8.357  47.685 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 33.56988   22.39792   1.499    0.135    
## open        -0.78181    0.04625 -16.904   <2e-16 ***
## high         0.82471    0.04780  17.252   <2e-16 ***
## low          0.94989    0.04681  20.294   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.87 on 247 degrees of freedom
## Multiple R-squared:  0.9951, Adjusted R-squared:  0.995 
## F-statistic: 1.668e+04 on 3 and 247 DF,  p-value: < 2.2e-16
# xem kết quả kiểm định Breusch-Godfrey
print(kqkinhteluong$kdkhuyetat)
## 
##  Breusch-Godfrey test for serial correlation of order up to 1
## 
## data:  kqhoiquy
## LM test = 1.9552, df = 1, p-value = 0.162
# xem kết quả dự báo
print(kqkinhteluong$kddubao)
##          Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
## 36.85714        3966.84 3906.476 4027.204 3874.521 4059.159
## 37.00000        3966.84 3881.472 4052.208 3836.281 4097.399
## 37.14286        3966.84 3862.286 4071.394 3806.938 4126.742
## 37.28571        3966.84 3846.111 4087.569 3782.201 4151.479
## 37.42857        3966.84 3831.861 4101.819 3760.407 4173.273
## 37.57143        3966.84 3818.978 4114.702 3740.704 4192.976
## 37.71429        3966.84 3807.131 4126.549 3722.586 4211.094