#input data
dta<-read.table("C:/Users/Ching-Fang Wu/Documents/data/boxoffice.txt",h=T)
#exam structure
str(dta)
## 'data.frame': 32 obs. of 2 variables:
## $ GrossBoxOffice: num 95.3 86.4 119.4 124.4 154.2 ...
## $ year : int 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 ...
總計有32年的歷年票房數據,起始年為1976年。
head(dta)
## GrossBoxOffice year
## 1 95.3 1976
## 2 86.4 1977
## 3 119.4 1978
## 4 124.4 1979
## 5 154.2 1980
## 6 174.3 1981
tail(dta)
## GrossBoxOffice year
## 27 844.8 2002
## 28 865.8 2003
## 29 907.2 2004
## 30 817.5 2005
## 31 866.6 2006
## 32 895.4 2007
#劃出時間序列圖,檢視票房的時間趨勢圖
plot(dta$year,dta$GrossBoxOffice,
ylab="Gross Box Office(in million $)",xlab="Year")
grid(lty=2)
with(dta, points(year, GrossBoxOffice, type="o", pch=16))
票房隨著時間增加而有上升趨勢。
#dta<-dta %>%
#mutate(dta,Year_75 = 1:32 ) #新增變數
#str(dta)
單獨跑這一條式子都沒有問題,但是按Knit後,會顯示有錯誤:沒有這個函數 %>%
#建立lm
#m1<-lm(formula = GrossBoxOffice ~ Year_75, data = dta)
#summary(m1)
平均而言,每增加一年,票房會增加29.534 million。
#knitr::kable(summary(m1<-lm(formula = GrossBoxOffice ~ Year_75, data = dta))$coefficients)
#DW test
#lmtest::dwtest(GrossBoxOffice ~ Year_75,
#data=dta)
#acf(residuals(m1))
#建立gls
#install.packages("gls")
#m2<-gls(formula= GrossBoxOffice ~ Year_75, data=dta, method="ML",correlation=corAR1(form = ~ Year_75))
#summary(m2)
Warning in install.packages : package ‘gls’ is not available (for R version 4.0.2) Error in gls(formula = GrossBoxOffice ~ Year_75, data = dta, method = “ML”, : 沒有這個函數 “gls” 這裡試了很多次,還是這樣….
#The END