library(readr)
hw1 <- read_csv("C:/Users/tony8/Desktop/hw1.csv")
## Rows: 24 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): 日期
## dbl (2): 國泰金月報酬率, 臺灣加權指數月報酬率
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(hw1)
## # A tibble: 6 × 3
## 日期 國泰金月報酬率 臺灣加權指數月報酬率
## <chr> <dbl> <dbl>
## 1 2005/8/31 -3.77 -4.41
## 2 2005/9/30 0.98 1.41
## 3 2005/10/31 -4.68 -5.79
## 4 2005/11/30 2.88 7.62
## 5 2005/12/31 -1.98 5.56
## 6 2006/1/31 0 -0.25
X=hw1$臺灣加權指數月報酬率
Y=hw1$國泰金月報酬率
\(\bar{X}\)=1.7054167
Xbar=mean(X)
\(\bar{Y}\)=1.8691667
Ybar=mean(Y)
\(S_{XX}\)=407.6101958
sxx=sum((X-Xbar)^2)
\(S_{YY}\)=1171.8803833
syy=sum((Y-Ybar)^2)
\(S_{XY}\)=462.1733083
sxy=sum((X-Xbar)*(Y-Ybar))
\(r\)=0.6687144
兩者呈現正相關,因此當X愈高時Y愈高,所以支持論點
correlation=sxy/(sqrt(sxx)*sqrt(syy));correlation
## [1] 0.6687144
attach(hw1)
plot(X,Y)
abline(lm(Y~X))
cor(X,Y)
## [1] 0.6687144
2.某一學院之招生主管從全部新生中選取120位學生,用以進行美國大學測驗成績(ACT)與期末平均等成績(GPA)兩者間關係之研究與預測工作,假設一階迴歸模型適合如下之研究資料:
CH01PR19 <- read.csv("C:/Users/tony8/Downloads/CH01PR19.txt", sep="")
head(CH01PR19)
## GPA ACT
## 1 3.897 21
## 2 3.885 14
## 3 3.778 28
## 4 2.540 22
## 5 3.028 21
## 6 3.865 31
a.求出\(\beta_0\)與\(\beta_1\)的最小平方估計並寫出所估計之迴歸函數。
z=lm(GPA~ACT,data = CH01PR19)
z
##
## Call:
## lm(formula = GPA ~ ACT, data = CH01PR19)
##
## Coefficients:
## (Intercept) ACT
## 2.11405 0.03883
\(\hat{\beta}_0\)=2.1140493
\(\hat{\beta}_1\)=0.0388271
迴歸模型 : \(GPA = 2.1140493+0.0388271ACT\)
b.畫出估計之迴歸函數與資料點,同時觀察配適是否良好。
library(ggplot2)
ggplot(CH01PR19,aes(x=ACT,y=GPA))+
geom_point()+
geom_smooth(method = "lm",formula = y~x,se=F,color="red")+
labs(x="ACT",y="GPA",title = "迴歸函數圖")+
theme(plot.title=element_text(size=12,hjust=0.5,vjust=0.5))
plot(GPA~ACT,data=CH01PR19)
abline(z)
由圖可知,配適程度為較弱的正相關。
c.當大學測驗成績\(X\)=30,求出新生期末平均等第成績之點估計值。
\(2.1140493+0.0388271 \times 30\)
d.當大學測驗成績提高1分時,求出平均反映改變量之點估計值。
\(2.1140493+0.0388271 \times +1\)
3.一種生醫研究用物質以每箱1000小瓶工運給使用者,10次空運紀錄如下: 其中X表示轉機次數,Y表示破損瓶數,假設一階迴歸模型(1.1)適合此資料,
CH01PR21 <- read.csv("C:/Users/tony8/Downloads/CH01PR21.txt", sep="")
head(CH01PR21)
## amplue transfer
## 1 16 1
## 2 9 0
## 3 17 2
## 4 12 0
## 5 22 3
## 6 13 1
a.求出所估計之迴歸函數,並畫出估計之迴歸函數與資料點,同時觀察配適是否良好。
c=lm(amplue~transfer,data = CH01PR21)
c
##
## Call:
## lm(formula = amplue ~ transfer, data = CH01PR21)
##
## Coefficients:
## (Intercept) transfer
## 10.2 4.0
ggplot(CH01PR21,aes(x=transfer,y=amplue))+
geom_point()+
geom_smooth(method = "lm",formula = y~x,se=F,color="red")+
labs(x="transfer",y="amplue",title = "迴歸函數圖")+
theme(plot.title=element_text(size=12,hjust=0.5,vjust=0.5))
plot(amplue~transfer,data=CH01PR21)
abline(c)
迴歸模型 : \(amplue = 10.2+4transfer\)
b.當轉機次數\(X\)=1時,求出平均破損瓶數之點估計值。
\(10.2+4 \times 1\)
c.估計轉機兩次比轉機一次所增加之平均破損瓶數。
\((10.2+4 \times 2)\)-\((10.2+4 \times 1)\)
d.驗證所配適之迴歸直線通過\((\bar{X},\bar{Y})\)。