From 41137cb2f556b8c645c338470f49c4e45dd404d3 Mon Sep 17 00:00:00 2001 From: emmaSkarstein <36135547+emmaSkarstein@users.noreply.github.com> Date: Wed, 11 Mar 2020 11:58:57 +0100 Subject: [PATCH 1/2] Changed all cases of df[,column-name] to df[[column-name]] in order to avoid problems when subsetting tibbles (without changing behavior for regular data frames). --- R/MakeBinomStack.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/R/MakeBinomStack.R b/R/MakeBinomStack.R index 6e56c51..b2f2336 100644 --- a/R/MakeBinomStack.R +++ b/R/MakeBinomStack.R @@ -26,7 +26,7 @@ MakeBinomStack=function(data, observs, tag="points", intercept=TRUE, mesh, presn if(length(presname)>1) stop("more than one name given for presences column") if(length(trialname)>1) stop("more than one name given for number of trials column") if(!presname%in%names(observs@data)) stop(paste(presname," not in names of presences data frame", sep="")) - if(!is.logical(observs@data[,presname]) & !trialname%in%names(observs@data)) + if(!is.logical(observs@data[[presname]]) & !trialname%in%names(observs@data)) stop(paste(trialname," not in names of presences data frame", sep="")) if(is.null(coordnames)) coordnames <- colnames(data@coords) @@ -36,21 +36,21 @@ MakeBinomStack=function(data, observs, tag="points", intercept=TRUE, mesh, presn } NearestCovs <- GetNearestCovariate(points=observs, covs=data) - if(InclCoords) { data@data[,coordnames] <- data@coords } - if(intercept) NearestCovs@data[,paste("int",tag,sep=".")] <- 1 # add intercept + if(InclCoords) { data@data[[coordnames]] <- data@coords } + if(intercept) NearestCovs@data[[paste("int",tag,sep=".")]] <- 1 # add intercept if(!is.null(polynoms)) { NearestCovs <- AddDistToRangeToSpatialPoints(data = NearestCovs, polynoms = polynoms, scale=scale) } # If presences are Boolean, reformat - if(is.logical(observs@data[,presname])) { - observs@data[,presname] <- as.integer(observs@data[,presname]) - observs@data[,trialname] <- rep(1, nrow(observs@data)) + if(is.logical(observs@data[[presname]])) { + observs@data[[presname]] <- as.integer(observs@data[[presname]]) + observs@data[[trialname]] <- rep(1, nrow(observs@data)) } # Projector matrix from mesh to data. projmat <- inla.spde.make.A(mesh, as.matrix(NearestCovs@coords)) # from mesh to point observations - stk.binom <- inla.stack(data=list(resp=cbind(NA,observs@data[,presname] ), Ntrials=observs@data[,trialname]), A=list(1,projmat), tag=tag, + stk.binom <- inla.stack(data=list(resp=cbind(NA,observs@data[[presname]] ), Ntrials=observs@data[[trialname]]), A=list(1,projmat), tag=tag, effects=list(NearestCovs@data, list(i=1:mesh$n))) stk.binom From 4f4f8f0e62d5040a6ca2f90d9c462624bc92a749 Mon Sep 17 00:00:00 2001 From: emmaSkarstein <36135547+emmaSkarstein@users.noreply.github.com> Date: Wed, 18 Mar 2020 18:41:26 +0100 Subject: [PATCH 2/2] Changed back to original subsetting some places. Changed back subsetting all places except where we check class of observs@data[[presname]], as previous changes seemed to cause one error when running the tests. Now I think we will avoid the problems with tibbles, but this also means that the dataframe subsetting is inconsistent throughout the code. --- R/MakeBinomStack.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/R/MakeBinomStack.R b/R/MakeBinomStack.R index b2f2336..a8b336e 100644 --- a/R/MakeBinomStack.R +++ b/R/MakeBinomStack.R @@ -36,21 +36,21 @@ MakeBinomStack=function(data, observs, tag="points", intercept=TRUE, mesh, presn } NearestCovs <- GetNearestCovariate(points=observs, covs=data) - if(InclCoords) { data@data[[coordnames]] <- data@coords } - if(intercept) NearestCovs@data[[paste("int",tag,sep=".")]] <- 1 # add intercept + if(InclCoords) { data@data[,coordnames] <- data@coords } + if(intercept) NearestCovs@data[,paste("int",tag,sep=".")] <- 1 # add intercept if(!is.null(polynoms)) { NearestCovs <- AddDistToRangeToSpatialPoints(data = NearestCovs, polynoms = polynoms, scale=scale) } # If presences are Boolean, reformat if(is.logical(observs@data[[presname]])) { - observs@data[[presname]] <- as.integer(observs@data[[presname]]) - observs@data[[trialname]] <- rep(1, nrow(observs@data)) + observs@data[,presname] <- as.integer(observs@data[,presname]) + observs@data[,trialname] <- rep(1, nrow(observs@data)) } # Projector matrix from mesh to data. projmat <- inla.spde.make.A(mesh, as.matrix(NearestCovs@coords)) # from mesh to point observations - stk.binom <- inla.stack(data=list(resp=cbind(NA,observs@data[[presname]] ), Ntrials=observs@data[[trialname]]), A=list(1,projmat), tag=tag, + stk.binom <- inla.stack(data=list(resp=cbind(NA,observs@data[,presname] ), Ntrials=observs@data[,trialname]), A=list(1,projmat), tag=tag, effects=list(NearestCovs@data, list(i=1:mesh$n))) stk.binom