#R-help to exercise 3.1 in BSS
# Read the data into a dataframe and give names to the variables:
cafe<-read.table("http://www.math.uio.no/avdc/kurs/STK4900/data/exer3_1.dat")
names(cafe)<-c("no","sale")
# Take a look at the data (make sure they correspond to those given in the exercise):
cafe
# Attach the dataframe (making the variables available):
attach(cafe)
# Make a plot of sale as a function of the number or dispensers:
plot(no,sale)
# Inspect the plot. How is the relation between the number of dispensers and the coffee sale?
# Fit a straight line and draw it on the plot:
linfit<-lm(sale~no)
linfit
abline(linfit)
# How well does the straight line describe the relation between the number of dispensers and the coffee sale?
# Fit a second order polynomial (note that inside lm-command, we have to write the second order term inside I( ), # otherwise the sign ^ ?will be misinterpreted by R):
lm(sale~no+I(no^2))
# Compute and draw the fitted second order polynomial:
x<-seq(0,7,0.1)
koef<-lm(sale~no+I(no^2))$coef
koef
lines(x,koef[1]+koef[2]*x+koef[3]*x^2,lty=2)
# Do the straight line or the second order polynomial provide the best description of the relation between the number of dispensers and the coffee sale?