#Final lab-Griffin Pugh #Clear the work space: rm(list=ls()) #Install the proper packages install.packages(“unmarked”) library(unmarked) library(lattice) #Load the data setwd(paste0(“C:\Users\gpugh\Downloads”)) Squirrels=read.csv(“Squirrels.csv”) head(Squirrels) #Parameters: vegtype=factor(Squirrels\(Vegtype) elevation.data=Squirrels\)Elevation_m elevation = scale(elevation.data) distance.road=Squirrels$DistanceRoad_m #Adjusting time parameter time0=Squirrels[,58:107] time.tmp=time0[!is.na(time0)] # Function to convert military time to decimal hours convertMilitaryTimeToDecimal <- function(military_time) { # Extract hours and minutes hours <- as.numeric(substr(military_time, 1, nchar(military_time) - 2)) minutes <- as.numeric(substr(military_time, nchar(military_time) - 1, nchar(military_time)))
# Convert minutes to decimal decimal_minutes <- minutes / 60
# Calculate total decimal hours decimal_hours <- hours + decimal_minutes
return(decimal_hours) }
time0[!is.na(time0)] <- convertMilitaryTimeToDecimal(time.tmp) hist(time0[!is.na(time0)]) time=time0 time[is.na(time0)]=runif(length(time[is.na(time0)]), 0 ,24) time #Response data: y=Squirrels[,4:53] head(y) day=matrix(rep(1:ncol(y),each=nrow(y)),nrow(y),ncol(y)) day2=day^2 #Formatting for unmarked: siteCovs=data.frame(vegtype,elevation,distance.road) obsCovs=list(time=time, date=day, date2=day2) Squirrels.occ=unmarkedFrameOccu(y=y,siteCovs=siteCovs, obsCovs = obsCovs) summary(Squirrels.occ) #{p(.),psi(.)} - null model #Model 1 model1=occu(~1 ~1, data=Squirrels.occ) summary(model1) #Model 1: AIC logLik_value1 <- logLik(model1) k1 <- length(coef(model1)) aic_value1 <- -2 * logLik_value1 + 2 * k1 print(aic_value1) #NULL and vegtype model: #Model 2 model2=occu(~1 ~vegtype, Squirrels.occ, starts=c(-8.37,7.67,7.27,7.96,8.37,7.67,0,-1.9)) summary(model2) #Model 2: AIC logLik_value2 <- logLik(model2) k2 <- length(coef(model2)) aic_value2 <- -2 * logLik_value2 + 2 * k2 print(aic_value2) #NULL and elevation #Model 3 model3=occu(~1 ~elevation, data=Squirrels.occ) summary(model3) #Model 3: AIC: logLik_value3 <- logLik(model3) k3 <- length(coef(model3)) aic_value3 <- -2 * logLik_value3 + 2 * k3 print(aic_value3) #NULL and road distance #Model 4 model4=occu(~1 ~distance.road, data=Squirrels.occ) summary(model4) #Model 4: AIC: logLik_value4 <- logLik(model4) k4 <- length(coef(model4)) aic_value4 <- -2 * logLik_value4 + 2 * k4 print(aic_value4) #Null and vegtype + Elevation #Model 5 model5=occu(~1 ~elevation+vegtype, data=Squirrels.occ) summary(model5) #Model 5: AIC: logLik_value5 <- logLik(model5) k5 <- length(coef(model5)) aic_value5 <- -2 * logLik_value5 + 2 * k5 print(aic_value5) #NULL and elevation + road distance #Model 6 model6=occu(~1 ~elevation+distance.road, data=Squirrels.occ) summary(model6) #Model 6: AIC: logLik_value6 <- logLik(model6) k6 <- length(coef(model6)) aic_value6 <- -2 * logLik_value6 + 2 * k6 print(aic_value6) #NULL and vegtype + distance to road #Model 7 model7=occu(~1 ~distance.road+vegtype, data=Squirrels.occ, starts=c(-0.88,-0.0059,1.943,0.123,2.205,1.336,0.8655,-7.362,-1.9)) summary(model7) #Model 7: AIC: logLik_value7 <- logLik(model7) k7 <- length(coef(model7)) aic_value7 <- -2 * logLik_value7 + 2 * k7 print(aic_value7) #NULL and elevation + road distance + vegtype #Model 8 model8=occu(~1 ~elevation+vegtype+distance.road, data=Squirrels.occ) summary(model8) #Model 8: AIC: logLik_value8 <- logLik(model8) k8 <- length(coef(model8)) aic_value8 <- -2 * logLik_value8 + 2 * k8 print(aic_value8) #Time and NULL: #Model 9 model9=occu(~time ~1, data=Squirrels.occ) summary(model9) #Model 9: AIC: logLik_value9 <- logLik(model9) k9 <- length(coef(model9)) aic_value9 <- -2 * logLik_value9 + 2 * k9 print(aic_value9) #Time and Veg #Model 10 model10=occu(~time ~vegtype, data=Squirrels.occ, starts=c(-8.60,7.90,7.50,8.19,8.60,7.90,0,-1.1677,-0.0638)) summary(model10) #Model 10: AIC: logLik_value10 <- logLik(model10) k10 <- length(coef(model10)) aic_value10 <- -2 * logLik_value10 + 2 * k10 print(aic_value10) #Time and elevation #Model 11 model11=occu(~time ~elevation, data=Squirrels.occ) summary(model11) #Model 11: AIC: logLik_value11 <- logLik(model11) k11 <- length(coef(model11)) aic_value11 <- -2 * logLik_value11 + 2 * k11 print(aic_value11) #Time and road distance: #Model 12 model12=occu(~time ~distance.road, data=Squirrels.occ) summary(model12) #Model 12: AIC: logLik_value12 <- logLik(model12) k12 <- length(coef(model12)) aic_value12 <- -2 * logLik_value12 + 2 * k12 print(aic_value12) #Time and elevation/veg #Model 13 model13=occu(~time ~elevation+vegtype, data=Squirrels.occ, starts=c(-1.9366,0.8978,1.8286,0.0369,1.8565,0.6610,1.9343,-8.2540, -1.542,-0.033)) summary(model13) #Model 13: AIC: logLik_value13 <- logLik(model13) k13 <- length(coef(model13)) aic_value13 <- -2 * logLik_value13 + 2 * k13 print(aic_value13) #Time and elevation/road distance #Model 14 model14=occu(~time ~elevation+distance.road, data=Squirrels.occ) summary(model14) #Model 14: AIC: logLik_value14 <- logLik(model14) k14 <- length(coef(model14)) aic_value14 <- -2 * logLik_value14 + 2 * k14 print(aic_value14) #Time and veg/road distance #Model 15 model15=occu(~time ~distance.road+vegtype, data=Squirrels.occ) summary(model15) #Model 15: AIC: logLik_value15 <- logLik(model15) k15 <- length(coef(model15)) aic_value15 <- -2 * logLik_value15 + 2 * k15 print(aic_value15) #Time and veg/elevation/distance #Model 16 set.seed(4) model16=occu(~time ~vegtype+elevation+distance.road, data=Squirrels.occ, starts=c(-0.65322,2.29225,-0.82985,2.32522,-0.012432,1.30517, -5.62168,0.85593,-0.00596,-1.541,-0.033)) summary(model16) #Model 16: AIC: logLik_value16 <- logLik(model16) k16 <- length(coef(model16)) aic_value16 <- -2 * logLik_value16 + 2 * k16 print(aic_value16) #AIC Values Table: AIC_Values=c(aic_value1,aic_value2,aic_value3,aic_value14,aic_value5,aic_value6, aic_value7,aic_value8,aic_value9,aic_value10,aic_value11, aic_value12,aic_value13,aic_value14,aic_value15,aic_value16) AIC_Values DeltaAIC=AIC_Values-aic_value12 DeltaAIC weights=round( exp(-1/2DeltaAIC)/ (sum(exp(-1/2DeltaAIC))), 2) weights #Creating the table for all the models: model=c(“model1”, “model2”, “model3”, “model4”, “model5”, “model6”, “model7”, “model8”, “model9”, “model10”, “model11”, “model12”, “model13”, “model14”, “model15”, “model16”) parameters = c(k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16) parameters table <- data.frame( Model = model, AIC = AIC_Values, DeltaAIC = DeltaAIC, Weights = weights, Parameters = parameters ) table #TOP Model analysis: summary(model12) coef(model12) confint(model12,level=0.95,type=‘det’) confint(model12,level=0.95,type=‘state’) #Estimating Occupancy prob. based on distance.road mean_value_S <- mean(distance.road, na.rm = TRUE) mean_value_S backTransform(linearComb(model12, coefficients = c(1,180.1154),‘state’)) min_Value_S <- min(distance.road, na.rm = TRUE) min_Value_S backTransform(linearComb(model12, coefficients = c(1,0),‘state’)) max_Value_S <- max(distance.road, na.rm = TRUE) max_Value_S backTransform(linearComb(model12, coefficients = c(1,887),‘state’)) #Estimating Detection Prob. based on time:
backTransform(linearComb(model12, coefficients = c(1,6.00),‘det’))
backTransform(linearComb(model12, coefficients = c(1,12.00),‘det’))
backTransform(linearComb(model12, coefficients = c(1,18.00),‘det’)) backTransform(linearComb(model1, coefficients = c(1),‘state’)) #Distance to road plot pred.road=seq(0,887,1) pred.road logit.psi=-0.28779-0.00368pred.road psi.pred=exp(logit.psi)/(1+exp(logit.psi)) plot(pred.road,psi.pred,type=‘l’, xlab=“Distance from road”, ylab=“Occupancy Prob.”) #Time plot: pred.time=seq(0.00,24.00,0.10) pred.time logit.p=-1.3564-0.0479(pred.time) p.pred=exp(logit.p)/(1+exp(logit.p)) plot(pred.time,p.pred,type=‘l’, xlab=“Time”, ylab=“Detection Prob.”) #Further analysis: Addition of time^2 time2=time^2 ObsCovs=list(time=time,date=day,date2=day2,time2=time2)
Squirrels.occ=unmarked::unmarkedFrameOccu(y=y,siteCovs=siteCovs, obsCovs=ObsCovs) summary(Squirrels.occ) time2=time^2 occu(~time+time2 ~distance.road, Squirrels.occ)
time.pred=seq(0.00, 24.00, 1.00) time2.pred=time.pred^2
logit.p=-3.7933+0.5088time.pred -0.0243time2.pred p.pred=exp(logit.p)/(1+exp(logit.p)) plot(time.pred,p.pred, xlab=“Time (Military)”, ylab= “Detection Probability”, type=‘line’)