###Lecture 31 - Comparing ANOVA to Regression models when x can be treated either way ###Import data - Note that x variable is continuous, but multiple samples at each x level datum=read.csv(file.choose()) head(datum) ### Plot relationship - notice that relationship between x and y may be non-linear plot(PreyDens~Mosquitos,data=datum) ###Analyze data treating x variable as continuous variable results1=lm(PreyDens~Mosquitos,data=datum) summary(results) ###Analyze data treating x variable as Categorical variable # First convert x variable from number to group datum$MoCat=as.factor(datum$Mosquitos) #Creates a new variable called "MoCat" that is the same as ### Mosquitos, but that R will treat as categorical. #run analysis results2=lm(PreyDens~MoCat,data=datum) summary(results2) ###Test null hypothesis that additional parameters all equal zero (i.e., simpler model is adequate) anova(results1,results2) ###Note that the null hypothesis is rejected if the relationship is nonlinear ###Note that we fail to reject the null hypothesis if the relationship is linear ### BONUS: How to fit a quadratic curve to data when x is continuous results3=lm(PreyDens~Mosquitos+I(Mosquitos^2),data=datum) anova(results3,results) #compare model 3 to model 1 (model 1 is simpler) anova(results2,results3) #compare model 2 to model 3 (model 3 is simpler)