Deadline : 5/5 pm12:00 繳交作業時請上傳html檔 (需要Knit!!)

目標 : 使用喙長(culmen_length_mm)預測身體重量(body_mass_g) 使用資料集 : penguins.csv

# ==============================================================================
# 讀取資料, hint: 使用read.csv("檔案路徑")
#請於下列開始填寫程式碼
pengiuns <- read.csv("C:/Users/Berkin Lai/Downloads/penguins.csv")

# 查看前六筆資料
#請於下列開始填寫程式碼
head(pengiuns)
##   culmen_length_mm culmen_depth_mm flipper_length_mm body_mass_g
## 1             39.1            18.7               181        3750
## 2             39.5            17.4               186        3800
## 3             40.3            18.0               195        3250
## 4             36.7            19.3               193        3450
## 5             39.3            20.6               190        3650
## 6             38.9            17.8               181        3625
# ==============================================================================

Step 0 : Take a look at the data

# ==============================================================================
# 繪製散佈圖
#請於下列開始填寫程式碼
library(ggplot2)
## Warning: 套件 'ggplot2' 是用 R 版本 4.4.2 來建造的
#繪製散佈圖,x軸是喙長,y軸是身體重量
ggplot(pengiuns, aes(x=culmen_length_mm, y=body_mass_g))+
   geom_jitter()

# ==============================================================================

Step 1 : Correlation Matrix

library(ggcorrplot)
## Warning: 套件 'ggcorrplot' 是用 R 版本 4.4.3 來建造的
# ==============================================================================
# 請繪製相關係數矩陣
#請於下列開始填寫程式碼
corr <- cor(pengiuns[,1:4]) 
ggcorrplot(corr)

#
ggcorrplot(corr,
           method = 'circle',
           type = 'full',
          title = '相關係數矩陣',
           colors = c(rgb(0.8,1,1), rgb(0.8,0.6,1), rgb(1,0,0.4)),
           lab = TRUE)

# ==============================================================================

Step 2 : Linear regression model

# ==============================================================================
# 請建立回歸模型
#請於下列開始填寫程式碼
model <- lm(formula= culmen_length_mm ~ body_mass_g, data=pengiuns)
fit <- summary(model)
fit
## 
## Call:
## lm(formula = culmen_length_mm ~ body_mass_g, data = pengiuns)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.1251  -3.0434  -0.8089   2.0711  16.1109 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.690e+01  1.269e+00   21.19   <2e-16 ***
## body_mass_g 4.051e-03  2.967e-04   13.65   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.394 on 340 degrees of freedom
## Multiple R-squared:  0.3542, Adjusted R-squared:  0.3523 
## F-statistic: 186.4 on 1 and 340 DF,  p-value: < 2.2e-16
# ==============================================================================

Step 3 : Plot regression line

# ==============================================================================
# 請繪製散布圖 + 趨勢線
#請於下列開始填寫程式碼
ggplot(pengiuns, aes(x = culmen_length_mm, y = body_mass_g)) +     
    geom_point() +  
    geom_smooth(method = "lm", formula = y ~ x, col = "red") +  
    ggtitle("regression")

# ==============================================================================