haotu : an open lab notebook

2018/02/01

R Markdown Resources

Filed under: R, R Markdown, R Stats — Tags: , , , , — S @ 09:10

cheatsheets here https://www.rstudio.com/resources/cheatsheets/

online tutorial

http://rmarkdown.rstudio.com/lesson-1.html

chapter in r for data science

http://r4ds.had.co.nz/r-markdown.html

 

Advertisements

2018/01/31

save the first (or any) element of a string split

Filed under: errors in R, Manipulate Data in R, R, R, R Stats — Tags: , — S @ 06:58
dirs<-matrix(ncol = 2,data=unlist(strsplit(out$`Full Name`,split=",")),byrow = TRUE)[,1]

2018/01/29

Replace . in string in R

Filed under: errors in R, Manipulate Data in R, R, R, R Stats — Tags: — S @ 07:54
gsub(x = ff,pattern = "[.]",replacement="")

2018/01/25

dplyr column names with spaces, how to index

Filed under: Manipulate Data in R, R, R, R Stats — Tags: , , — S @ 09:12

backtick it!

`my name`

https://stackoverflow.com/questions/22842232/dplyr-select-column-names-containing-white-space

2018/01/03

All Pairwise Combinations of Rownames from a Square Matrix

Filed under: R, R, R spatial, R Stats, Uncategorized — Tags: , , , — S @ 12:37
combn(rownames(my.square.matrix), m=2, FUN=paste, collapse = "-", simplify = T)

2017/12/11

read file from parent folder of current working directory

Filed under: Manipulate Data in R, R, R, R Stats, Uncategorized — Tags: , — S @ 08:10

If  your working directory is C:Parent/Child

Then to read a file from Parent use ..

file="../MY_FILE.RData"

https://stackoverflow.com/questions/24319168/how-to-move-backward-parent-folder

2017/07/18

Make ggplot look like base plot in R

Filed under: ggplot, R, R graphics — Tags: , , , , , , — S @ 04:46
myplot + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))
myplot + theme_bw() + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))

here

 

 

 

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)
Older Posts »

Blog at WordPress.com.