mydata<-read.table("./fish.csv", header = TRUE, sep= "," , dec=";")
head (mydata[c("Weight","Length1","Height", "Width")],)
## Weight Length1 Height Width
## 1 242 23.2 11.52 4.02
## 2 290 24 12.48 4.3056
## 3 340 23.9 12.3778 4.6961
## 4 363 26.3 12.73 4.4555
## 5 430 26.5 12.444 5.134
## 6 450 26.8 13.6024 4.9274
Here is the imported data on the fish market with choosen variables for the regresion analysis. The data was retrieved from: https://www.kaggle.com/datasets/vipullrathod/fish-market/data and this dataset is a collection of data related to various species of fish and their characteristics.
In total we have 159 obesrvations. Our unit of observation is fish. Here’s a description of each column in the dataset that we used for this analysis:
Weight: This column represents the weight of the fish. It is a numerical variable that is typically measured in grams. This is our dependent variable
Length1: This column represents the first measurement of the fish’s length. It is a numerical variable, typically measured in centimetres. This is our independent/explanatory variable
Height: This column represents the height of the fish. It is a numerical variable, typically measured in centimetres. This is our explanatory variable.
Width: This column represents the width of the fish. Like the other numerical variables, it is also typically measured in centimetres.This is our explanatory variable which will be changed into the categorical for the purpopse of the analysis.
We expect that eachof variables influence the dependent variable positively.
#since the data was in chr we changed the firstly in numeric and one in categorical
mydata$Length1<- as.numeric(mydata$Length1)
mydata$Height<- as.numeric(mydata$Height)
mydata$Width<- as.numeric(mydata$Width)
mydata$Weight<- as.numeric(mydata$Weight)
mydata$WidthF<-as.factor(ifelse(mydata$Width>4, 1,0))
summary(mydata[c("Weight","Length1","Height","Width")])
## Weight Length1 Height Width
## Min. : 0.0 Min. : 7.50 Min. : 1.728 Min. :1.048
## 1st Qu.: 120.0 1st Qu.:19.05 1st Qu.: 5.945 1st Qu.:3.386
## Median : 273.0 Median :25.20 Median : 7.786 Median :4.248
## Mean : 398.3 Mean :26.25 Mean : 8.971 Mean :4.417
## 3rd Qu.: 650.0 3rd Qu.:32.70 3rd Qu.:12.366 3rd Qu.:5.585
## Max. :1650.0 Max. :59.00 Max. :18.957 Max. :8.142
Here we can see that average weigth of the fishes inspected is 398.3 grams. Average length after the first measurement is 26.25cm and average height of fishes was 8.971cm. Average width of fishes inspected is 4.417 and for the purpopse of the assignement, this variable will be transformed into categorical with width higher than 4cm being noted as 1 and with 0 if it is lower than 4cm. Overveiw is in the table bellow:
head (mydata[c("Weight","Length1","Height", "Width","WidthF")],)
## Weight Length1 Height Width WidthF
## 1 242 23.2 11.5200 4.0200 1
## 2 290 24.0 12.4800 4.3056 1
## 3 340 23.9 12.3778 4.6961 1
## 4 363 26.3 12.7300 4.4555 1
## 5 430 26.5 12.4440 5.1340 1
## 6 450 26.8 13.6024 4.9274 1
#install.packages("pastecs")
library(pastecs)
round(stat.desc(mydata[c("Weight","Length1","Height")], basic = FALSE), 2)
## Weight Length1 Height
## median 273.00 25.20 7.79
## mean 398.33 26.25 8.97
## SE.mean 28.39 0.79 0.34
## CI.mean.0.95 56.07 1.57 0.67
## var 128148.48 99.93 18.37
## std.dev 357.98 10.00 4.29
## coef.var 0.90 0.38 0.48
summary(mydata["WidthF"])
## WidthF
## 0:68
## 1:91
For the categorical variable here we can see that there are 68 fishes with width less than 4cm and 91 of them with width higher than 4cm.
After examining the data the block model is made with one basic and one expended model:
Weight= b0 + b1Lenght1 + b2Height
Weight= b0 + b1Lenght1 + b2Height + b3Width
With scatterplot we can see the relationship between numerical variables:
#install.packages("car")
library(car)
## Loading required package: carData
scatterplotMatrix(mydata[,c(2,3,6)])
The first row shows no specific problem with linearity and other plots might suggest the possible heteroscadacity (for the height and other two numerical varibales) or mulitceolinearity of data (for example for the weight and length). Just in case the multicolinearityis checked with VIF statistics.
#install.packages("car")
library(car)
fit<- lm(Weight ~ Length1 + Height+ WidthF,
data = mydata)
vif (fit)
## Length1 Height WidthF
## 2.397202 2.167859 3.068287
Based on the data we can conclude that we do not have problem with multicolinearity since all of our data are under 5.
Next we check the histogram of standardized residuals to check for outliers and we use cook’s distance histogram to check for the units of high impact.
fit<- lm(Weight ~ Length1 + Height ,
data = mydata)
mydata$StdResid<- round(rstandard(fit),3)
mydata$CooksD<-round(cooks.distance(fit),3)
hist(mydata$StdResid,
xlab = "Standardized residuals",
ylab = "Frequency",
main="Histogram of standardized residuals")
Our results suggest possible problem with normailty. To check data the Shapiro-Wilk normality test is done.
shapiro.test(mydata$StdResid)
##
## Shapiro-Wilk normality test
##
## data: mydata$StdResid
## W = 0.96519, p-value = 0.0004929
In our case the p value is 0.0004929 which is less than 0.05, hence we reject the null hypothesis and conclude that data is not normaly distributed.
hist(mydata$CooksD,
xlab = "Cook's distances",
ylab = "Frequency",
main = "Histogram of Cook's distances")
On the histogram of Cook’s distance we can wee the units of high impact after the 0.25 and next step is to find the problematic data and remove it from the set.
#we added id variable
ID<-1:nrow(mydata)
mydata<-cbind(ID, mydata)
head(mydata[order(mydata$StdResid), c("ID","StdResid")], 3)
## ID StdResid
## 138 138 -2.302
## 135 135 -2.260
## 132 132 -2.200
head(mydata[order(-mydata$StdResid), c("ID","StdResid")], 5)
## ID StdResid
## 143 143 3.140
## 144 144 2.726
## 145 145 2.682
## 124 124 2.188
## 73 73 2.088
head(mydata[order(-mydata$CooksD), c("ID", "CooksD")], 8)
## ID CooksD
## 143 143 0.337
## 145 145 0.271
## 144 144 0.254
## 138 138 0.081
## 139 139 0.070
## 135 135 0.061
## 73 73 0.046
## 134 134 0.043
After checking the data we can se that our units with ID number 143, 144 and 145 are the ones that created the problem both with normality and presented the units of higher impact.
#install.packages("pastecs")
#install.packages("stats")
#install.packages("base")
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
##
## recode
## The following objects are masked from 'package:pastecs':
##
## first, last
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mydata<-mydata %>%
filter(!ID %in% c(143, 144,145))
fit<- lm(Weight ~ Length1 + Height ,
data = mydata)
mydata$StdFitted<-scale(fit$fitted.values)
#after the removal of problematic units we will check the relationship between Standardised Residuals and Standardised fitted values.
library(car)
scatterplot(StdResid~StdFitted,
ylab="Standardized residuals",
xlab="Standardized fitted values",
smooth= FALSE,
regLine=FALSE,
boxplots= FALSE,
data= mydata)
Based on the satterplot we can assume that there is a problem with posible heteroskedasticity. To check that, we preform Breusch pagan test.
#install.packages("olsrr")
library(olsrr)
##
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
##
## rivers
ols_test_breusch_pagan(fit)
##
## Breusch Pagan Test for Heteroskedasticity
## -----------------------------------------
## Ho: the variance is constant
## Ha: the variance is not constant
##
## Data
## ----------------------------------
## Response : Weight
## Variables: fitted values of Weight
##
## Test Summary
## ------------------------------
## DF = 1
## Chi2 = 8.619167
## Prob > Chi2 = 0.003326439
Based on the results of the test we can conclude that there is a Heteroskedasticity present and reject the null hypothesis (p<0.005).
In the next step we will check the regression function with done findings and than the one with the correction or heteroskedasticity.
summary(fit)
##
## Call:
## lm(formula = Weight ~ Length1 + Height, data = mydata)
##
## Residuals:
## Min 1Q Median 3Q Max
## -255.44 -69.44 -29.01 72.36 317.45
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -449.338 27.536 -16.318 <2e-16 ***
## Length1 22.823 1.369 16.666 <2e-16 ***
## Height 26.701 2.888 9.246 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 114.5 on 153 degrees of freedom
## Multiple R-squared: 0.8732, Adjusted R-squared: 0.8715
## F-statistic: 526.8 on 2 and 153 DF, p-value: < 2.2e-16
Weight = -449.338 + 22.823Length1 + 26.701Height
#install.packages("estimatr")
library(estimatr)
fit1 <- lm_robust(Weight ~ Length1 + Height,
se_type ="HC1",
data = mydata)
summary(fit1)
##
## Call:
## lm_robust(formula = Weight ~ Length1 + Height, data = mydata,
## se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) -449.34 32.116 -13.991 3.621e-29 -512.79 -385.89 153
## Length1 22.82 1.871 12.197 2.468e-24 19.13 26.52 153
## Height 26.70 3.113 8.579 1.000e-14 20.55 32.85 153
##
## Multiple R-squared: 0.8732 , Adjusted R-squared: 0.8715
## F-statistic: 392.2 on 2 and 153 DF, p-value: < 2.2e-16
The estimates are same but the Pr(>|t|) is much lower after the correction.
Based on the sampled data, we reject H0 (p<0.001) and conclude that at least one of the explanatory variables (Height, Length1) impact Weight.
The Adjusted R-squared value of 0.8715 indicates us that the model explains 87.15% of the variance in dependent variable.
For every one-ceentimeter increase in length, the weight of fish increases by approximately 22.82 grams, ceteris paribus. For every one-centimeter increase in Height, the weight of fish increases by approximately 26.7 grams.
Next is the model with categorical variable in:
fit2 <-lm_robust(Weight ~ Length1 + Height + WidthF,
se_type = "HC1",
data = mydata)
summary(fit2)
##
## Call:
## lm_robust(formula = Weight ~ Length1 + Height + WidthF, data = mydata,
## se_type = "HC1")
##
## Standard error type: HC1
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
## (Intercept) -462.04 41.423 -11.1543 1.743e-21 -543.88 -380.20 152
## Length1 23.49 2.289 10.2631 4.197e-19 18.97 28.01 152
## Height 27.64 3.190 8.6632 6.307e-15 21.33 33.94 152
## WidthF1 -22.55 29.769 -0.7574 4.500e-01 -81.36 36.27 152
##
## Multiple R-squared: 0.8736 , Adjusted R-squared: 0.8711
## F-statistic: 276.3 on 3 and 152 DF, p-value: < 2.2e-16
In this results we can see that all units are the same regarding Width which we just added; commenting on our categorical variable -
If the width of fish is less than or equal to 4cm the weight of fish decreases by 22.55 grams, holding all other variables constant. the analysis with categorical variable has same Adjusted R-squared: 0.8711 and p value.