Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Tuesday, October 17, 2017

R - conversion

Don't directly convert factor to numeric! Change to characters first!

df$a = as.numeric(df$a) ---> NOT GOOD!

df$a = as.numeric(as.character(df$a)) ---> GOOD!


Monday, October 9, 2017

Sanitize U.S. States Names


# load data
StateData = read.csv('65States.pip', sep="|", col.names = c("FullName", "Abbr"))
StateFullName = toupper(StateData$FullName)
StateAbbr = as.vector(StateData$Abbr)

# define a function
sanitizeState = function(inputcol, StateFullName, StateAbbr){
  match = amatch(inputcol, StateFullName, maxDist=1)
  inputcol[!is.na(match)] = StateAbbr[na.omit(match)]
  return (inputcol)
}

# use the function
df$State = sanitizeState(gls$State,StateFullName, StateAbbr )

Monday, January 11, 2016

Remove multiple objects with similar names in R

I have a bunch of objects in the R environment, with names as: file_01, file_02, ... etc.
To remove these objects by using just one line of code, here's one solution:
rm(file_01, file_02,...)
However, if the number of files is too large, it is not practical to type in all the file names. Here's a solution to remove these objects, again by using just one line of code:
rm(list=ls(patter='file'))

Thursday, October 1, 2015

Aggregate R dataframe with multiple conditions and multiple outputs

t_ag = aggregate(cbind(CPC, PM25)~SiteID+Date+Session+stType2, 
                 data = t,
                 FUN = mean)

Tuesday, June 9, 2015

A simple example of mixed effects model using simulated data in R

First let's make the dataframe.

rm(list=ls())
library(lme4)
set.seed(1)
ID = c(rep('A',10), rep('B', 10), rep('c', 10))
x = c(1:10)
ya = x + 1 + rnorm(10)
yb = x + 2 + rnorm(10)
yc = x + 3 + rnorm(10)
df <- data.frame="" id="" x="rep(x,3)," y="c(ya,yb,yc))</font">

Then let's do liner regression.
mdl.lm <- span=""> lm(y~x, data=df)
coef(mdl.lm)

## (Intercept)           x
##    2.073487    1.001631

The linear regression cannot treat each ID seperately. It provides an overall intercept (2.073487) and overall slope (1.001631) for all the 30 data points.

Next, let's build a random-intercept mixed effects model.

mdl.mix.RI <- span=""> lmer(y~x+(1+1|ID),data=df, REML=F)
coef(mdl.mix.RI)

## $ID
##   (Intercept)        x
## A    1.287210 1.001631
## B    2.211162 1.001631
## c    2.722090 1.001631
##
## attr(,"class")
## [1] "coef.mer"


The above model can treat each ID differently, so the Intercept for each ID are different.

Last, let's build a random-intercept and random-slope mixed effects model.

mdl.mix.RIS <- span=""> lmer(y~x+(1+x|ID),data=df, REML=F)
coef(mdl.mix.RIS)

## $ID
##   (Intercept)         x
## A   0.9373545 1.0650834
## B   2.2293276 0.9929275
## c   3.0537802 0.9468823
##
## attr(,"class")
## [1] "coef.mer"


This model also treat each ID differently, and it gives different intercept and slope for each ID.

Thursday, May 21, 2015

r apply function to list

http://stackoverflow.com/questions/30339652/r-apply-correlation-function-to-a-list/30340905#30340905


Tuesday, May 19, 2015

annotate p value in italicized font in ggplot

label = substitute(italic(p) == a, list(a=0.371) )
ggplot() + annotate('text', label=as.character(as.expression(label)), x=10, y=10, parse=T)


Wednesday, April 29, 2015

Relationship between Pearson correlation coefficient and the r-squared from linear regression

Simply put, if you take the square root of the r-squared value, which can be easily obtained in Excel, the outcome equals to the Pearson correlation coefficient.

Here's my R code to test this:


x<-rnorm c="" font="" nbsp="">
y<-rnorm c="" font="">
mdl <- font="" lm="" x="" y=""># linear regression model
r = summary(mdl)$r.squared # the r-squared value from linear regression
rp = cor.test(x,y, method='pearson')$estimate # rp is the Pearson correlation coefficient
sqrt(r) == rp

Monday, June 23, 2014

Annotating subscript in R plots

I need to annotate one of my figure like this:

(b) PM2.5


And this is a way to do it in R with ggplot2 package:

b <- b="" font="">
annotate("text", x=-3.5, y=0.12, label=deparse(b),parse=TRUE)

Not sure about the details behind all the parameters though. Thanks to BondedDust for answering this question.

my-alpine and docker-compose.yml

 ``` version: '1' services:     man:       build: .       image: my-alpine:latest   ```  Dockerfile: ``` FROM alpine:latest ENV PYTH...