rm(list = ls())
library(readr)
library(ggplot2)
#body weight and heart weight
setwd("c:/users/Paul/Documents/Rworksmsu")
heartwtdata<- read.csv(file="heartwtdatahw3.csv")
heartwtdata
##    Seal weight  heart
## 1     2   27.5  127.7
## 2     1   24.3   93.2
## 3     3   22.0   84.5
## 4     4   18.5   85.4
## 5     5   28.0  182.0
## 6     6   23.8  130.0
## 7     7   18.5   98.8
## 8     8   18.0  117.3
## 9    10   34.0  180.0
## 10    9   28.5  150.4
## 11   11   35.8  145.5
## 12   12   30.3  140.0
## 13   13   33.0  168.1
## 14   14   42.0  205.0
## 15   15   37.5  205.0
## 16   16   55.0  320.9
## 17   17   50.5  255.0
## 18   18   57.5  370.0
## 19   19   67.5  465.0
## 20   20   53.5  332.8
## 21   23   73.0  449.0
## 22   21   54.5  282.5
## 23   22   68.5  355.0
## 24   24   74.0  310.0
## 25   25   68.0  532.0
## 26   26   97.0  667.0
## 27   27  101.0  440.0
## 28   28  128.0 1012.0
## 29   29   95.0  505.0
## 30   30  179.0 1075.0
str(heartwtdata)
## 'data.frame':    30 obs. of  3 variables:
##  $ Seal  : int  2 1 3 4 5 6 7 8 10 9 ...
##  $ weight: num  27.5 24.3 22 18.5 28 23.8 18.5 18 34 28.5 ...
##  $ heart : num  127.7 93.2 84.5 85.4 182 ...
heartwtdata$heartwt<-(heartwtdata$heart)
heartwtdata$bodywt<-(heartwtdata$weight)
str(heartwtdata)
## 'data.frame':    30 obs. of  5 variables:
##  $ Seal   : int  2 1 3 4 5 6 7 8 10 9 ...
##  $ weight : num  27.5 24.3 22 18.5 28 23.8 18.5 18 34 28.5 ...
##  $ heart  : num  127.7 93.2 84.5 85.4 182 ...
##  $ heartwt: num  127.7 93.2 84.5 85.4 182 ...
##  $ bodywt : num  27.5 24.3 22 18.5 28 23.8 18.5 18 34 28.5 ...
#Let's visualize the scatter plots of the heartwt vs bodywt dataset
#ai heartwt vs bodywt
heartplot1<-ggplot(heartwtdata,aes(x=bodywt,y=heartwt))+geom_point()
heartplot1

#aii heartwt vs log(bodywt)
heartplot2<-ggplot(heartwtdata,aes(x=log(bodywt),y=heartwt))+geom_point()
heartplot2

#aiii log(heartwt) vs bodywt
heartplot3<-ggplot(heartwtdata,aes(x=bodywt,y=log(heartwt)))+geom_point()
heartplot3

#aiv log(heartwt) vs log(bodywt)
heartplot4<-ggplot(heartwtdata,aes(x=log(bodywt),y=log(heartwt)))+geom_point()
heartplot4

#Next, lets model the different variables forms above
heartwtmodel1<- lm(heartwt~bodywt,data=heartwtdata)
heartwtmodel2<- lm(heartwt~log(bodywt),data=heartwtdata)
heartwtmodel3<- lm(log(heartwt)~bodywt, data=heartwtdata)
heartwtmodel4<- lm(log(heartwt)~log(bodywt),data=heartwtdata)
plot(heartwtmodel1)

plot(heartwtmodel2)

plot(heartwtmodel3)

plot(heartwtmodel4)

summary(heartwtmodel1)
## 
## Call:
## lm(formula = heartwt ~ bodywt, data = heartwtdata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -178.306  -30.681    0.867   23.815  217.139 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -42.1379    24.0199  -1.754   0.0903 .  
## bodywt        6.5391     0.3662  17.856   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 72.33 on 28 degrees of freedom
## Multiple R-squared:  0.9193, Adjusted R-squared:  0.9164 
## F-statistic: 318.8 on 1 and 28 DF,  p-value: < 2.2e-16
summary(heartwtmodel2)
## 
## Call:
## lm(formula = heartwt ~ log(bodywt), data = heartwtdata)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -185.09  -77.38  -27.50   50.66  315.13 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1089.81     131.96  -8.259 5.48e-09 ***
## log(bodywt)   368.23      34.14  10.785 1.77e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 112.1 on 28 degrees of freedom
## Multiple R-squared:  0.806,  Adjusted R-squared:  0.799 
## F-statistic: 116.3 on 1 and 28 DF,  p-value: 1.769e-11
summary(heartwtmodel3)
## 
## Call:
## lm(formula = log(heartwt) ~ bodywt, data = heartwtdata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.72097 -0.16108  0.02722  0.16653  0.53892 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 4.534957   0.096749   46.87  < 2e-16 ***
## bodywt      0.017688   0.001475   11.99 1.51e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2913 on 28 degrees of freedom
## Multiple R-squared:  0.837,  Adjusted R-squared:  0.8312 
## F-statistic: 143.8 on 1 and 28 DF,  p-value: 1.515e-12
summary(heartwtmodel4)
## 
## Call:
## lm(formula = log(heartwt) ~ log(bodywt), data = heartwtdata)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.31487 -0.09182  0.00002  0.11685  0.32051 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.20434    0.21131   5.699 4.12e-06 ***
## log(bodywt)  1.12615    0.05467  20.597  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1796 on 28 degrees of freedom
## Multiple R-squared:  0.9381, Adjusted R-squared:  0.9359 
## F-statistic: 424.3 on 1 and 28 DF,  p-value: < 2.2e-16