haotu : an open lab notebook

2017/06/22

Automatic MIT License for R Packages

Filed under: errors in R, R, R Stats — Tags: , — S @ 07:01
devtools::use_mit_license()

2016/10/28

The relationship between VIF and R2 (r squared)

Filed under: Math and Stats, R, R Stats — Tags: , , — S @ 09:08

Variance Inflation Factor (VIF) is a common simple stat used to quantify multicollinearity in least squares regressions. It is calculated for each covariate in a regression, with higher values meaning that the covariate is more colinear with the other covariates. It technically measures “how much the variance (the square of the estimate’s standard deviation) of an estimated regression coefficient is increased because of collinearity.” The equation is:

{\displaystyle \mathrm {VIF_{i}} ={\frac {1}{1-R_{i}^{2}}}}

where R2i is from the regression of the covariate i on all the other covariates. The problem is where to draw the cutoff? Is a VIF > 2.5 too high? >5? or how about VIF>10, all have been used as cutoffs. Here is a figure of R2 vs VIF. As you can see, a cuttoff of 2.5 is an R2 of 0.60 and 10 is 0.90! While statistically, you could perhaps get away with these high inflations, what does it mean for your particular question? If you are dealing with a relationship among covariates that is as strong as 0.90, can you really be sure that the model and your interpretations are valid?

rplot

 

 

#VIF function
r<-function(x){1-(1/x)} #r is R2 and x is VIF
x<-seq(1,15,.1) #seq of VIFs
y<-sapply(x,r) #seq of R2
#plot
par(las=1)
plot(x,y,type="l",xlab="VIF",ylab="R2 of regression of focal covariate on all other covariates")
# common VIF cutoffs = 2.5, 5, 10
ly<-c(y[x==2.5],y[x==5],y[x==10])
lx<-c(2.5,5,10)
segments(lx,0,lx,ly,col="red")
segments(lx,ly,0,ly,col="red")

2016/07/14

aggregate by removing NA

Filed under: Manipulate Data in R, R, R Stats, Uncategorized — Tags: — S @ 08:43
na.collapse<-function(x)
{
 x.<-unique(x[!is.na(x)])
   if(length(x.)==0)
 {
   return(NA)
   } else {
     if(length(x.)==1){
     return(x.)
   } else {
     return(paste(x.,collapse="|"))
   }
  }
}
na.collapse(x)

2016/06/24

save rda data file with compression

Filed under: errors in R, R, R, R Stats — Tags: , , , , — S @ 06:36
save(mydata,file="mydata.rda",compress="xz")

find non-ascii in R

Filed under: errors in R, R, R, R Stats, Uncategorized — Tags: , , , , — S @ 06:34
tools::showNonASCII(readLines("myfiles.R"))

2016/04/25

assign nearest point to a polygon

Filed under: R, R spatial, R Stats, Uncategorized — S @ 11:31

 

wer<-SpatialPointsDataFrame(data.frame(x.subset[,"longitude"],x.subset[,"latitude"]),data=data.frame(x.subset))
n <- length(wer)
nearest <- character(n)
for (i in seq_along(nearest)) {
 nearest[i] <- names(polys)[which.min(gDistance(wer[i,], polys, byid=TRUE))]
}

This may give you warnings if your code is not projected into a planar coordinate system

http://stackoverflow.com/questions/26308426/how-do-i-find-the-polygon-nearest-to-a-point-in-r

2016/04/19

number of layers in raster stack

Filed under: R, R graphics, R spatial, R Stats, Uncategorized — S @ 07:08
dimension(stacked)[3]

2016/04/18

stack rasters with different extents etc..

Filed under: arcmap, R, R, R spatial, R Stats, Uncategorized — S @ 13:42
two<-resample(two,one)
stack(one,two)

raster cell area size 1 degree cell size km2

Filed under: R, R spatial, R Stats, Uncategorized — Tags: , — S @ 08:14

For a 1×1 degree cell size raster

r <- raster(ncol=360, nrow=180) #or just the default r<-raster()
area(r)
area(r)$values

 

http://gis.stackexchange.com/questions/177622/r-calculate-raster-cell-size-in-map-units

 

http://gis.stackexchange.com/questions/29734/how-to-calculate-area-of-1-x-1-degree-cells-in-a-raster

2016/04/12

R project to R package Rstudio

Filed under: R, R Stats, Uncategorized — S @ 08:18

This is OK

http://www.r-bloggers.com/building-a-package-in-rstudio-is-actually-very-easy/

  1. Start with a Github repository. This will have a set of R sourcecode files, which collectively do something interesting.
  2. Within RStudio, create a new project, using your local Github repository as the project’s source directory.
  3. Within the “Build Tools” section of the “Project Options” make sure that you’ve selected “Package” from the “Project Build Tools” dropdown box.
  4. In that same dialog box, enter the “–no-examples” flag of the “Check Package” build options. (At this stage, you probably don’t have any examples composed. The default assumes that you do. I don’t know why this is.)
  5. If RStudio, didn’t create one, create a subdirectory in your project folder called “R”. Move all your code here. Github will reflect the deletion and new file creation.
  6. Create a DESCRIPTION file. There are numerous sources which explain how to do this.
  7. Make sure your code doesn’t have any errors in it. Easiest way to do that is to source all of them.
  8. Build the package.

A nice Video here:

Also this devtools checklist

https://www.rstudio.com/wp-content/uploads/2015/06/devtools-cheatsheet.pdf

 

Older Posts »

Create a free website or blog at WordPress.com.