##### General Code for calculating predictions and prediction intervals in R ### import the data datum=read.csv(file.choose()) #imports data head(datum) #displays the column headings of data plus first 6 rows ### Run analysis - Cannot make predictions until model has been run results=lm(Y~X,data=datum) #runs a linear model between 'Y' and 'X' from 'datum', call results 'results # 'Y' is the response in your excel file. BE SURE TO CHANGE THE NAME # 'X' is the x-variable in your excel file. BE SURE TO CHANGE THE NAME # in example, I used results=lm(Biomass~Rainfall,data=datum) summary(results) # gives output. ##### Generating Predictions ### Generate a new data frame of X-values that you want predictions for # in this case, I'm just getting predictions across the x-range summary(datum) ###get min and max values for X help(seq) ### help file for seq function - useful for creating sequences of values x=seq(from=min,to=max,by=interval) # where 'min' is minimum value of X, #'max' is maximum value of x # 'interval' is the distance between individual x values # be sure to input values for min, max, and interval # in example, I used x=seq(from=0,to=10,by=0.1) NewX=data.frame(X=x) ### where 'X' is the x-variable from your excel file. BE SURE TO CHANGE THE NAME ###Example: NewX = data.frame(Biomass=x) where 'x' is the sequence of values head(NewX) ###make sure it made the new data.frame predictions=predict(results,NewX,interval="prediction") ### generates a vector of y predictions and prediction intervals predictions=as.data.frame(predictions) ###convert 'predictions' to a data.frame predictions$X=NewX$X ###Add column of X values to predictions ### plot predictions for easy viewing plot(Y~X,data=datum) ### plot original X and Y data - replace X and Y as appriopriate lines(fit~X,data=predictions) ### plots best fit line as a line lines(lwr~X,data=predictions) ### plots lower prediction interval lines(upr~X,data=predictions) ### plots upper prediction interval