Labs/Lab3/mylab3.R

spruce.df = read.csv("SPRUCE.csv")
head(spruce.df)

# Scatterplot Height~BHDiameter
windows()
with(spruce.df,  {
  layout(matrix(1:2,nr=2))
  plot(
    Height~BHDiameter,
    bg="Blue",
    pch=21,
    cex=1.2,
    xlim=c(0, 1.1*max(BHDiameter)),
    ylim=c(0, 1.1*max(Height))
  )
})


# Lowess smoother scatter plot using layout and trendscatter()
library(s20x)
layout(matrix(1:3,nr=3,nc=1,byrow=TRUE))
layout.show(3)
trendscatter(Height~BHDiameter,f=0.5, data=spruce.df)
trendscatter(Height~BHDiameter,f=0.6, data=spruce.df)
trendscatter(Height~BHDiameter,f=0.7, data=spruce.df)

### Use lm() or linear model to create obj and scatter plot with abline()
spruce.lm <- lm(Height~BHDiameter, data = spruce.df)
summary(spruce.lm)
windows()
plot(Height~BHDiameter, data = spruce.df)
abline(spruce.lm)

## Task 4
layout(matrix(1:4,nr=2,nc=2,byrow=TRUE))

### First graph

with(spruce.df,
     plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter)))
)
abline(spruce.lm)

### Second graph

with(spruce.df,
     plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter)))
)
abline(spruce.lm)


### Third graph

with(spruce.df,{
  segments(BHDiameter,Height,BHDiameter,yhat)
})

yhat=fitted(spruce.lm)


with(spruce.df,
     plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter)))
)

#make nieve model
with(spruce.df, abline(h=mean(Height)))
abline(spruce.lm)
#make the explained deviations (explained by the model)
with(spruce.df, segments(BHDiameter,mean(Height),BHDiameter,yhat,col="Red"))

### Fourth graph
with(spruce.df,
     plot(Height~BHDiameter,bg="Blue",pch=21,ylim=c(0,1.1*max(Height)),xlim=c(0,1.1*max(BHDiameter)))
)
with(spruce.df,abline(h=mean(Height)))
with(spruce.df, segments(BHDiameter,Height,BHDiameter,mean(Height),col="Green"))

# right?
with(spruce.df,
     plot(Height~BHDiameter,bg="Blue")
)
abline(spruce.lm)

# wrong
obj = lm(Height~BHDiameter, data = spruce.df)
plot(Height~BHDiameter, data = spruce.df)
abline(spruce.lm)
agracy2246/MATH4753grac0009 documentation built on April 26, 2020, 9:39 a.m.