# Scott Merrill # Oct 16 # Quantitative Thinking # Cow weight rm(list = ls(all = TRUE)) ########### # generating a line with error ########### outflow.nitrogen = sample(0:60,100,replace = TRUE) data.error = rnorm(n=100,mean=0,sd=4) slope = .075 cow.weight = outflow.nitrogen*slope + data.error plot(outflow.nitrogen,cow.weight, main = "Amount of Nitrogen in overland irrigation outflow vs Percent change in cow weight by field") mod1= glm(cow.weight~outflow.nitrogen + data.error) summary(mod1) #fitted line abline(mod1$coefficients, lwd = 4, col = "green") # zero line abline(0,0, lwd = 4, col = "red") # plot distances plot(cow.weight, col = "red", lwd = 2) points(mod1$residuals, col = "green", lwd = 2) abline(0,0, lwd = 4, col = "black") # plot sorted distances plot(sort(cow.weight), col = "red", lwd = 2) points(sort(mod1$residuals), col = "green", lwd = 2) abline(0,0, lwd = 4, col = "black") # statistically speaking we care about the squared error error.zero.line = cow.weight^2 sum(error.zero.line) error.fit.line = mod1$residuals^2 sum(error.fit.line) # plotting the squared error plot(sort(error.zero.line), col = "red") points(sort(error.fit.line), col ="green") abline(0,0, lwd = 4, col = "black") summary(mod1) mtext(c("F-statistic, DF model, DF error ",summary(mod1)[10]), line = -20, side = 1)