Adam Hudson U2249314

Question 1

Firstly, we look at the weight of the diamonds. To understand the distribution of this variable we look at a boxplot of the data and the summary statistics below:

# Data
data <- read.csv("ST231data.csv")
boxplot(data$weight, horizontal = TRUE, main="Boxplot of weights", xlab="Weight", ylim=c(0,1.2),
col=c("white","white"), lwd=1)

summary(data$weight)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.2000  0.4100  0.7000  0.6523  0.9000  1.1100

Firstly, by looking at the numerical data, we can see that the median and the mean are close together which indicates that the middle point or balancing point of the data is close to the average. This means that our data partly resembles a symmetric distribution. Also, our interquartile range is 0.49 compared to a range of 0.9 which is not much more than half the range. This means that the middle 50% of the data so half the data comes in about half the range in the middle which indicates that the data is fairly evenly spread, almost similar to a uniform distribution.

Furthermore, if we look at the box plot we can see that the median is clearly to the right of the box but not by a highly significant margin, indicating that our data is a little skewed to the left. We can also gauge from the box plot that the data appears to be fairly uniformly spread as mentioned above, as the median as mentioned is slightly centralised with the IQR taking up about half the space. Furthermore we see from the boxplot that there are no outliers in the data set indicating that it is accurate. We can back this up in the numerical data where we see that all the points are within an IQR of the upper or lower quartile.

Now we have a look at the widths of the diamonds. Again we consider a boxplot of the data and the summary statistics of the widths shown below:

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   3.750   4.768   5.610   5.443   6.160   6.530

Again we can see from the summary statistics that the mean and the median are close together, which means that our data closely resembles a symmetric distribution. Furthermore, looking at the summary statistics we see that the IQR is 1.392 and the range is 2.78. Our ratio between the IQR and the range is very similar to that of the weights meaning that 50% of the data points occur in about 50% of the range which implies that the data could resemble a uniform distribution, although we cannot tell from these statistics if all the data peaks in a certain area of the range.

From the box plot we can see that the data wouldn’t resemble a uniform distribution as our box is well toward the right of the plot. With this and the fact that the median is slightly to the right of the box too indicates that the distribution is skewed to the left. By the fact that the left tail end of the boxplot is twice that of the right(and backed up by our summary statistics), we would expect that the values on the left could potentially be outliers. However from the boxplot and summary statistics we can see that the min value is within an IQR of the 1st quartile. Therefore, we have no outliers in the data, so we can take the whole thing to analyse the distribution.

Question 2a

When we look at these plots we want to be paying attention to certain features which will determine whether we have a good model or not. These are linearity(do our errors have a mean of 0), independence(are our errors and thus our response variables independent of each other), homoscedasticity(do our errors and thus our response variables have constant variables) and normality(do our errors and thus our response variables have a normal distribution).

We first compute the scatter plot of weight against width. Then we create our simple linear regression model from this. The scatter plot with the regression line is below:

model1 <- lm(data$weight ~ data$width)
plot(data$width, data$weight, main="Scatter of weight against width",xlab="Width", ylab="Weight", col="red", pch=4, cex=0.5, bg="lightblue")
abline(coef(model1), col="blue")

Then we present the residual plot of model A below:

plot(model1, 1, main="Model A residual plot", pch=4, cex=0.5, col="blue")

Firstly we will look at our scatter plot, the plot seems to have strongly positive correlated with the data appearing to be linear. In the plot we appear to have a few clusters of points at weights around 0.4,0.7 and 1.0 carats, but largely across the range it seems that a diamond is equally likely to be any weight(evenly distributed). There is a small dispersion from the regression line with the biggest occurring at the smallest weight values, we can also observe that the weight values tend to be above the line at low or high widths but below the line otherwise.

Furthermore, it follows to say that the regression line appears to fit the data we have well but doesn’t really capture the trend we have as the data curves slightly(width increasing at an increasing rate as width increases). We can also observe the y-intercept and gradient of the regression line but we cannot directly interpret what they mean from these graphs.

The residual plot shows us how far our points are from the regression line(our errors) for particular values of width and we can use this to detect if our assumptions our satisfied. The first thing we notice looking at the plot is that our data line constantly curves in a U shape, we also see that our vertical distance of points at each fitted value does not appear to stay relatively constant across the residual plot with much greater distances to the right than to the left. This means indicates that our model does not follow homoscedasticity so our errors do not have constant variance which means we need to address this area. Secondly, for our residual plot if our assumptions were satisfied we would expect our points to be randomly scattered about 0 but since we have a U shape this is not the case. It follows that the linearity assumption is not satisfied here meaning we need to address this area if we want our model to be accurate.

Independence appears to be apparent as our errors remain about constant in the residual plot therefore this does not need to be addressed. We now look at the final assumption which is normality. To check this from the residual plot would be difficult but we can easily make a QQ-plot and see how close to a straight line the data is to assess. We see that:

plot(model1, which = 2, pch=16, main="Model A QQ plot")

And we see the data is very close to the straight line so closely resembles a normal distribution, thus our normality assumption is satisfied and we do not need to address this area.

Question 2b

To improve model A, we can look at the residual analysis that we did in question 2a. We saw that we had an inverse-u shape which indicates a lack of linearity and the motivation here is to make that flat so we do satisfy linearity. We clearly need to change something to make this work but we must remember the context too,

We would expect direct proportionality between the weight and a function of the width of a diamond because naturally you would expect weight to increase as width increases because if width increased you expect the diamond’s volume to increase and since they are all the same material density would remain constant meaning weight would also increase. By looking at the round cut diamonds it seems fair to estimate the shape by a cone, as we have a circular top then diagonals down meeting to a point.

From the definition of the volume of a cone we have that. Volume \(\propto\) \(width^2\) x height which implies that. Weight \(\propto\) \(width^2\) x height. If we consider height as a constant since we do not know the heights of the diamonds, we can take logs of both sides to give us a linear model for this relationship of:

\(\log(\text{Weight}_i) = \beta_0 + \beta_1 \log(\text{Width}_i) + \epsilon_i \quad \text{for } i = 1, \ldots, 500.\)

Meaning that our original scale would have been:

\(Weight_i = \exp(\beta_0) \cdot Width_i^{\beta_1} \cdot \exp(\epsilon_i)\)

Now if our assumption that the diamonds resemble a cone is correct then we would expect \(\exp(\beta_0) = \text{height} \times \text{density} \times \frac{\pi}{12}\) as \(\text{radius} = \frac{\text{width}}{2}\) and \(\beta_1 = 2\). But since this model is linear and represents the shape of the diamond being that of cone to relate diamond width to weight, then this model should improve upon model A.

Question 2c

We now implement and look at the residual plot of model B, presented below:

logweight <- log(data$weight)
logwidth <- log(data$width)
B.model <- lm(logweight ~ logwidth)
plot(B.model, 1, main="Model B residual plot", pch=4, cex=0.5, col="blue")

On first glance our residual plot for B indicates that we have a much more suitable model for the data than we did for model A. We can see by the random array of points scattered that our model is going to satisfy independence. Then we can see that the y-value of our heights seem to average about 0 along the whole x-axis, which means that the model is satisfying the linearity assumption since this indicates the errors have a mean of 0. Furthermore, the y-range of values appears to be constant across every x-value except from 3 outliers near 0.2 residual value. This implies we have constant variance across our points as with randomness we would expect a few outliers in a large data set, so homoscedasticity is also satisfied. Finally, we check normality. To do this again we examine whether the QQ-plot is similar to the straight line. We observe:

plot(B.model, which = 2, pch=16, main="Model B QQ plot")

This is similar to the straight line, which means that our errors resemble that of a normal distribution, thus normality is satisfied. Therefore, all our assumptions for the linear model are satisfied, our model is thus an accurate valid one for the data given.

Question 2d

Clearly by looking at our residual plot we must solve the problems of heteroscedasticity and non-linearity. We first look at the problem of heteroscedasticity, this means we do not have constant variance across particular values of widths. Clearly to solve this problem we must transform the response variable, which in this case is the weights. To get a transformation that would work we need to decrease the differences in ranges of weights at particular widths, so we wish to apply concave functions for example square root, logarithm. Ignoring logarithm due to model B and by researching the heteroscedasticity I discovered the Box-Cox transformation, so we transform the weight as follows:

\(\frac{y^\lambda - 1}{\lambda}\)

Where \(\lambda\) is a constant value that we change until we get homoscedasticity. By changing this and viewing the residual plot we find that \(\lambda=0.1\) gives us constant variance.

Then by viewing the scatter plot of our transformed response variable with width we see that we have a slight increasing concave pattern. So to preserve constant variance but to solve linearity we transform the width to the power of numbers below one until we get an apparent linear relationship. It follows that we get a linear relationship at \(\text{width}^{0.4}\). So this is our new model C, the residual plot and scatter plots are below:

x <- (data$width)^0.4
lambda <- 0.1
y <- (((data$weight)^lambda)-1)/lambda
C.model <- lm(y ~ x)
plot(x, y, pch=4, cex=0.5, col="red")

plot(C.model, 1, main="Model C residual plot", pch=4, cex=0.5, col="blue")

Question 2e

Firstly, when comparing models A,B,C we disregard A as being the best model because if we go back to the residual analysis of A we found that the model does not satisfy the linearity and homoscedastic assumptions. Due to this our estimated coefficients might not accurately represent the true relationship and furthermore due to our assumptions not being met the interpretation of our results may be misleading or just incorrect, leading to inappropriate decisions if we decided to do something with the data.

So now we are left with just model B and model C, in which the residual plots appear to be extremely similar. Firstly, lets check that C satisfies the normality assumption by examining the QQ plot below:

plot(C.model, which = 2, pch=16, main="Model C QQ plot")

So model C also satisfies all the model assumptions so we cannot decide which model is preferable based off this. Furthermore, both models are about as complex as each other so we cannot make any further judgment on this. The first difference I note is that model B is much easier to interpret than model C, this is because model B was setup by considering the shape of the diamonds and how the width would affect the volume of it and therefore weight making it easy to understand. Whereas, it may be difficult to know what model C actually means since we almost just played about with transformations until we got to an accurate model. This may make model C a worse model than B even though we could change the value of lambda and the power that the width’s are put to, to give an almost perfectly accurate model.

But as mentioned above model C will be our most accurate representation of the data and will therefore give the best predictions, we can also look at goodness-of-fit statistics to see which model fits the data set better. We can look at the summary statistics of both models below:

summary(B.model)
## 
## Call:
## lm(formula = logweight ~ logwidth)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.16286 -0.03151  0.00183  0.02745  0.19414 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -5.68094    0.02605  -218.1   <2e-16 ***
## logwidth     3.06509    0.01541   198.9   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04978 on 498 degrees of freedom
## Multiple R-squared:  0.9876, Adjusted R-squared:  0.9875 
## F-statistic: 3.956e+04 on 1 and 498 DF,  p-value: < 2.2e-16
summary(C.model)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.152656 -0.028044  0.002258  0.026424  0.192517 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -7.80444    0.03709  -210.4   <2e-16 ***
## x            3.71984    0.01885   197.4   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0472 on 498 degrees of freedom
## Multiple R-squared:  0.9874, Adjusted R-squared:  0.9874 
## F-statistic: 3.896e+04 on 1 and 498 DF,  p-value: < 2.2e-16

In fact we can observe the multiple R-squared values for this, in which for model B we get 0.9876 compared to model C’s 0.9874. So in fact model B is a better fit for the data than C. However, as mentioned we can change the values in C to make it more accurate than B but this is a testament to the accuracy of B either way.

In conclusion, since B is still an extremely accurate model for the data, it is the preferable one since we can make valid interpretations from it to apply to real life-settings, whereas we may struggle to with C even though we can make accurate predictions with it.

Question 2f

To find quantitative interpretations of the slope coefficients that we have for model B it will be helpful to first look at the summary statistics. Listed below:

summary(B.model)
## 
## Call:
## lm(formula = logweight ~ logwidth)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.16286 -0.03151  0.00183  0.02745  0.19414 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -5.68094    0.02605  -218.1   <2e-16 ***
## logwidth     3.06509    0.01541   198.9   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04978 on 498 degrees of freedom
## Multiple R-squared:  0.9876, Adjusted R-squared:  0.9875 
## F-statistic: 3.956e+04 on 1 and 498 DF,  p-value: < 2.2e-16

From this we can see that the intercept is \(\beta_0=-5.68094\). And from question 2b we see that from the area of cone, in which the model is defined on that \(\exp(\beta_0) = \text{height} \times \text{density} \times \frac{\pi}{12}\). Thus if we take the exponential value of beta0 times it by 12 and divide it by pi we get:

\(\text{height} \times \text{density} = 0.013\)

Where height and density are that of the diamonds, this is our interpretation of the y-intercept. Now furthermore, from the summary statistics we see that\(\beta_1=3.06509\). To interpret this again we look back to a formula in question 2b, this time being \(Weight_i = \exp(\beta_0) \cdot Width_i^{\beta_1} \cdot \exp(\epsilon_i)\). We also have that for a ten percent increase in the width of a diamond gives it 1.1 times the width. So we compute that:

\(1.1^{\beta_1}=1.339\), means for a ten percent increase in the width of the diamond we expect a 34 percent increase in the weight.

Question 2g

We will have a look at the scatter plot weight against width logged as in model B and show it below:

plot(logwidth, logweight, main="Scatter of weight against width",xlab="Width", ylab="Weight", col="red", pch=4, cex=0.5, bg="lightblue")
abline(B.model, lwd=, col="blue")

We have also placed the line generated by model B on the scatter plot to illustrate the relationship prediction that B generates. From this we can clearly see that model B gives accurate predictions for the relationship between weight and width as the errors are small.

Adam Hudson U2249314