# Sample R Code for simple linear regression x=c(1,2,3,4) # enter x values y=c(1.1,2,3.5,4.2) # enter y values plot(x,y) # create scatterplot of x and y xbar=mean(x) # calculate sample mean of x print(xbar) ybar=mean(y) # calculate sample mean of y print(ybar) sx=sd(x) # calculate sample sd of x print(sx) sy=sd(y) # calculate sample sd of y print(sy) r=cor(x,y) # calculate sample correlation coefficient between x and y print(r) betahat=r*(sy/sx) # calculate sample estimate of slope print(betahat) alphahat=ybar-(betahat*xbar) # calculate sample estimate of intercept print(alphahat) summary(lm(y~x)) # check calculations with R's built-in function # abline(lm(y~x)) # Superimpose the fitted regression line on the scatterplot