###script R, version 23/05/2016 ###authors: Christine Plumejeaud-Perreau and Anne S. Philippe ###Method for cleaning the outliers, example for DM shells of bivalve species Macoma Balthica in site AIC and year 2013, from an hypothetical unclean dataset, "benthos_unclean" ###select the data to be cleaned up data<-subset(benthos_unclean, benthos_unclean$abr=="mac" & benthos_unclean$st_sit_id=="aic" & benthos_unclean$year==2013) data<-subset(data, data$number==1) #make sure you start the extrapolation based on an unfolded dataframe, where each row corresponds with a single individual plot(data$length, data$ShellDM)#plot shell masses ("ShellDM") as a function of shell length (length) ###identify the outliers visually based on the unique identifyier "id", and create a vector "v" of outliers text(data$ShellDM~data$length, labels=data$id, cex= 0.2, pos=4) v<-c(35666,54545,85655)#the list of id's here is just an example tmp.y <- log(data$ShellDM[!data$id%in%v]) #the relation between Shell Dry mass and length is exponential, here we build a model excluding outliers (id's contained in vector "v") tmp.x <- log(data$length[!data$id%in%v]) tmp <- lm(tmp.y~tmp.x) tmp$coef ##creates a list of two coefficients to use in the regression model ###Change the outlier values to NA values #data$ShellDM[!data$id%in%v]<-NA par(new=T) curve(exp(tmp$coef[1]+tmp$coef[2]*log(x)), from=min(data$length), to=max(data$length) , xlab="x", ylab="y", xaxt="n", yaxt="n",bty="n", col='red') ##add the curve to the plot to check that it fits well the data ###predict new values for outliers, and predict values for all NAs tmp.predict <- exp(tmp$coef[1]+tmp$coef[2]*log(data$length[is.na(data$ShellDM)])) #created the extrapolation model data$ShellDM[is.na(data$ShellDM)] <- tmp.predict #applies the model to all NAs in the dataset, including outliers that were transformed earlier into NAs summary(data$ShellDM) ##check that the dataset does not contain NAs for ShellDM anymore, or other outliers rm(tmp.y, tmp.y,v) #remove unecessary objects from the working environment