library(alr3)
## Loading required package: car
data("brains")
attach(brains)
head(brains)
##            BrainWt  BodyWt
## Arctic_fox  44.500   3.385
## Owl_monkey  15.499   0.480
## Beaver       8.100   1.350
## Cow        423.012 464.983
## Gray_wolf  119.498  36.328
## Goat       114.996  27.660

We should plot the two variables

plot(BrainWt,BodyWt)

There does not seem to be a linear relationship.

brainmod<- lm(BrainWt~BodyWt)
plot(brainmod)

We can see that there is not a linear relationship between the variables, we can tell by looking at the residual plot.

We can transform variables to make it more linear.

tranmod<- lm(sqrt(BrainWt)~ sqrt(BodyWt))
plot(tranmod)

This is slightly better but still not very good. we can also take the log of the functions.

logmod<-lm(log(BrainWt)~log(BodyWt))
plot(logmod)

This is much better and the residuals have a much more constant variablilty. This will be a much better model to fit the data linearly.

data("stopping")
attach(stopping)
head(stopping)
##   Speed Distance
## 1     4        4
## 2     5        2
## 3     5        4
## 4     5        8
## 5     5        8
## 6     7        7
plot(Distance~Speed)

This data looks a lot more linear but not completely.

speedmod<-lm(Distance~Speed)
summary(speedmod)
## 
## Call:
## lm(formula = Distance ~ Speed)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -25.410  -7.343  -1.334   5.927  35.608 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -20.1309     3.2308  -6.231 5.04e-08 ***
## Speed         3.1416     0.1514  20.751  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.77 on 60 degrees of freedom
## Multiple R-squared:  0.8777, Adjusted R-squared:  0.8757 
## F-statistic: 430.6 on 1 and 60 DF,  p-value: < 2.2e-16
plot(speedmod
     )

The residuals show we should try transforms.

speedmod.5<-lm(sqrt(Distance) ~ sqrt(Speed))
plot(speedmod.5)

These have a similar look to them so it is not much better than before.