random forest - How can I subsample a SpatialPointsDataFrame in R -


i working on running randomforest. i've imported point data representing used , unused sites , created raster stack raster gis layers. i've created spatialpointdataframe of used , unused points underlying raster values attached.

require(sp) require(rgdal) require(raster)  #my raster stack xvariables <- stack(rlist)  #rlist = list of raster layers     # reading in spatial used , unused points. ldata <- readogr(dsn=paste(path, "data", sep="/"), layer=used_avail) str(ldata@data)   #attach raster values point data. v <- as.data.frame(extract(xvariables, ldata)) ldata@data = data.frame(ldata@data, v[match(rownames(ldata@data), rownames(v)),]) 

next plan run random forest using data. problem is, have large data set (over 40,000 data points). need sub sample data having hard time figuring out how this. i've tried using sample() function think because have spatialpointsdatafram wont work? i'm new r , appreciate ideas.

thanks!

subsetting spatial*dataframe object simple; use

spsubset <- spobject[<sample_criterion>,] 

here's example load colleges in nj, grab random sample of size=20. there's example load states , grab "new jersey".

library(rgdal) set.seed(1) # random sample of nj colleges... samplesize=20 sppoints <- readogr(dsn=".",layer="nj_college_univ_nad83njsp") spsample <- sppoints[sample(1:length(sppoints),samplesize),]  # extract nj states tiger/line file states   <- readogr(dsn=".",layer="tl_2013_us_state") nj       <- states[states$name=="new jersey",] nj       <- sptransform(nj,crs=crs(proj4string(spsample)))  # render map nj.df    <- fortify(nj) library(ggplot2) ggplot() +   geom_path(data=nj.df, aes(x=long,y=lat, group=group))+   geom_point(data=as.data.frame(coordinates(sppoints)),               aes(x=coords.x1,y=coords.x2),colour="blue", size=3)+   geom_point(data=as.data.frame(coordinates(spsample)),               aes(x=coords.x1,y=coords.x2),colour="red", size=3)+   coord_fixed() + labs(x="", y="") + theme(axis.text=element_blank()) 

the tiger/line file of states can found here. nj college shapefile here.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -