-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaleobg.R
70 lines (59 loc) · 2.44 KB
/
paleobg.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
##function for cross-temporal background points
require(dismo)
require(raster)
paleobg = function(x, colNames=names(x), envFolder, n=10000){
### parameters ###
##x = data.frame with (at least) occurrences (lon, lat) and age
##colNames = names of the columns in x for lon, lat and age
##envFolder = path to predictor variables' folder
##n = desired number of background points
################
colNames = colNames
envFolder = envFolder
occTable = x[,colNames]
names(occTable) = c('lon','lat','age')
ages = unique(occTable$age)
ages = sample(ages, n, replace=TRUE)
bgData = data.frame()
vecNA = vector()
for ( age_i in unique(ages) ){
sampleSize = length(grep(age_i, ages))
current_occPts = occTable[ match(age_i, occTable$age), c('lon','lat')]
if(age_i %in% as.numeric(list.files(envFolder))){
envData = list.files(paste(envFolder,'/',age_i,sep=''), full.names=TRUE)
varNames = basename(list.files(paste(envFolder,'/',age_i,sep=''), full.names=TRUE))
varNames = gsub("\\..*", "", varNames)
}else{
warning("Atenção: algumas idades não possuem dados ambientais no directório. NAs produzidos.")
vecNA = append( vecNA, age_i )
next
}
if( (length(agrep("maxent", basename(envData), value = T)) > 0) ){
envData = envData[-c(agrep("maxent", basename(envData)))]
}
envData = stack(envData)
crs(envData) = CRS('+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0')
bgData_i = dismo::randomPoints(mask=envData[[1]], n=sampleSize, p=current_occPts)
bgData_i = data.frame(bgData_i)
bgData_i$age = age_i
names(bgData_i) = c('lon','lat','age')
bgData_i = round(bgData_i, 2)
bgData_i = unique(bgData_i)
bgData_i_vars = extract(envData, bgData_i[,c('lon','lat')])
bgData_i_vars = data.frame(bgData_i_vars)
names(bgData_i_vars) = varNames
bgData_i = data.frame(bgData_i, bgData_i_vars)
bgData = rbind(bgData, bgData_i)
}
#dealing with unexistnt ages
if ( length(vecNA) > 0 ){
matrixNA = matrix( data=rep(NA, length(vecNA)*max(2,ncol(bgData))), nrow=length(vecNA), ncol=max(2,ncol(bgData)) )
matrixNA = data.frame(matrixNA)
names(matrixNA) = unlist( list('data', names(bgData) )[ifelse(length(names(bgData))==0, 1, 2)] )
idxForAge = ifelse( ncol(matrixNA)==2, 1, grep('age', names(matrixNA)))
matrixNA[ ,idxForAge] = vecNA
bgData = rbind(bgData, matrixNA)
}
#output
return(bgData)
}