Helo, Welcome to My Rmd In this Rmd I would explore about Length and Weight Relationship
I want to know Length and Weight Relationship Bream Spesies with Regression Linear Model
It is important to find out the relationship of length and weight, in order to know which factors are the most influential, between internal and external factors
library(tidyverse)
library(ggthemes)
library(GGally)
library(dplyr)
library(MLmetrics)
library(lmtest)
library(car)
Dataset is from Kaggel.com “fish_market”
fish <- read.csv("Fish.csv")
head(fish)
glimpse(fish)
## Rows: 159
## Columns: 7
## $ Species <chr> "Bream", "Bream", "Bream", "Bream", "Bream", "Bream", "Bream",…
## $ Weight <dbl> 242, 290, 340, 363, 430, 450, 500, 390, 450, 500, 475, 500, 50…
## $ Length1 <dbl> 23.2, 24.0, 23.9, 26.3, 26.5, 26.8, 26.8, 27.6, 27.6, 28.5, 28…
## $ Length2 <dbl> 25.4, 26.3, 26.5, 29.0, 29.0, 29.7, 29.7, 30.0, 30.0, 30.7, 31…
## $ Length3 <dbl> 30.0, 31.2, 31.1, 33.5, 34.0, 34.7, 34.5, 35.0, 35.1, 36.2, 36…
## $ Height <dbl> 11.5200, 12.4800, 12.3778, 12.7300, 12.4440, 13.6024, 14.1795,…
## $ Width <dbl> 4.0200, 4.3056, 4.6961, 4.4555, 5.1340, 4.9274, 5.2785, 4.6900…
Karena mencari hubungan panjang berat, maka saya hanya memerlukan data berat dan panjang saja
fish <- fish %>%
select(Species, Weight, Length1, Length2, Length3)
head(fish)
Di Ilmu Perikanan, pengukuran panjang terbagi menjadi 3, Standard Length, Fork Length dan Total Length. Saya ubah terlebih dahulu sesuai dengan istilah yg sering di gunakan
fish <- fish %>%
rename(Standard_Length = Length1,
Fork_Length = Length2,
Total_Length = Length3)
anyNA(fish)
## [1] FALSE
ggcorr(fish, label = TRUE, label_size = 4, hjust = 0.7,)
## Warning in ggcorr(fish, label = TRUE, label_size = 4, hjust = 0.7, ): data in
## column(s) 'Species' are not numeric and were ignored
unique(fish$Species)
## [1] "Bream" "Roach" "Whitefish" "Parkki" "Perch" "Pike"
## [7] "Smelt"
Bream <- fish %>%
filter(Species == "Bream")
Perch <- fish %>%
filter(Species == "Perch")
boxplot((Bream$Total_Length), (Perch$Total_Length))
Bream <- Bream %>%
select(Weight,Total_Length)
set.seed(123)
RNGkind()
## [1] "Mersenne-Twister" "Inversion" "Rejection"
row_data <- nrow(Bream)
index <- sample(row_data, row_data*0.8)
Bream_train <- Bream[ index, ]
Bream_test <- Bream[ -index, ]
model_bream <- lm(Weight ~ Total_Length, Bream_train)
model_bream
##
## Call:
## lm(formula = Weight ~ Total_Length, data = Bream_train)
##
## Coefficients:
## (Intercept) Total_Length
## -1235.08 48.23
summary(model_bream)
##
## Call:
## lm(formula = Weight ~ Total_Length, data = Bream_train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -224.066 -27.709 -9.479 32.724 180.291
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1235.083 134.103 -9.21 1.14e-09 ***
## Total_Length 48.235 3.463 13.93 1.44e-13 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 73.84 on 26 degrees of freedom
## Multiple R-squared: 0.8818, Adjusted R-squared: 0.8773
## F-statistic: 194 on 1 and 26 DF, p-value: 1.444e-13
plot(Bream$Total_Length, Bream$Weight)
abline(model_bream$coefficients[1],model_bream$coefficients[2])
hist(model_bream$residuals)
bream_pred <- predict(model_bream, Bream_test)
MAE(bream_pred, Bream$Weight )
## [1] 223.0489
MAPE(bream_pred, Bream$Weight)
## [1] 0.3845915
MSE(bream_pred, Bream$Weight)
## [1] 78668.78
RMSE(bream_pred, Bream$Weight)
## [1] 280.4795
hist(model_bream$residuals)
shapiro.test(model_bream$residuals)
##
## Shapiro-Wilk normality test
##
## data: model_bream$residuals
## W = 0.93088, p-value = 0.06489
bptest(model_bream)
##
## studentized Breusch-Pagan test
##
## data: model_bream
## BP = 0.1262, df = 1, p-value = 0.7224
data.frame(prediction = model_bream$fitted.values,
residual = model_bream$residuals) %>%
ggplot(aes(prediction, residual)) +
geom_hline(yintercept = 0, color = "red2") +
geom_point( color = "skyblue4") +
theme_minimal()
Tidak dapat di ukur, karena prediktor hanya 1