######Simple linear regression###### datalr <- read.table("linreg.txt", h=T) attach(datalr) str(datalr) #Assumption 1: Linear and causal relationship between x and y plot(algae_density~phosphates) #Assumption 2: Independence of the residuals #####Model building##### linmodel <- lm(algae_density~phosphates, data=datalr) summary(linmodel) # y = 0.084 + 0.115*x #Assumption 3: Normal distribution of model residuals plot(linmodel,2) aov_residuals <- residuals(object = linmodel) shapiro.test(aov_residuals) #Assumption 4: Homogeneity of variance plot(linmodel,1) #####Data visualization###### #Plotting the data# install.packages(ggplot2) library(ggplot2) p1 <- ggplot(datalr, aes(x=phosphates, y=algae_density)) + geom_point(shape=1)+ # Use open circles geom_smooth(method=lm, se=T)+ # Add linear regression line with 95% CI theme_bw()+ labs(x = expression(paste("PO"[4]^3^-{}, " (µM/L)")), y = expression(paste("Algae density", " (g/L)"))) p1