Posts Tagged ‘ code ’

Summary stats for ABC

September 1, 2013
By
Summary stats for ABC

Introduction In the previous post I gave a very brief introduction to ABC, including a simple example for inferring the parameters of a Markov process given some time series observations. Towards the end of the post I observed that there were (at least!) two potential problems with scaling up the simple approach described, one relating […]

Read more »

“[” and “[[” with the apply() functions

August 20, 2013
By
“[” and “[[” with the apply() functions

Did you know you can use "[" and "[[" as function names for subsetting with calls to the apply-type functions? For example, suppose you have a bunch of identifier strings like "ZYY-43S-CWA3" and you want to pull off the bit before the first hyphen ("ZYY" in this case). (For code to create random IDs like […]

Read more »

Read the source code

August 6, 2013
By
Read the source code

The other day, there was a bit of a twitter conversation about qqline in R. It made me think: how exactly is the line produced by qqline chosen? I seemed to recall that the line was through the first and third quartiles. An advantage of R is that you can just type the name of […]

Read more »

Learning to code in R

April 26, 2013
By
Learning to code in R

It used to be that the one of the first decisions to make when learning to program was between compiled (e.g. C or FORTRAN) and interpreted (e.g. Python) languages. In my opinion these days one would have to be a masochist to learn with a compiled language: the extra compilation time and obscure errors are […]

Read more »

Beware of grep with a list

April 3, 2013
By
Beware of grep with a list

Another R tip: beware of as.character applied to a list. Really, beware of grep with a list: You might have thought that the result would be just 1, but grep expects a vector of character strings. If the input is not that, it uses as.character(). Since the result of that starts with "c(", grep finds […]

Read more »

apply vs for

April 3, 2013
By
apply vs for

It’s widely understood that, in R programming, one should avoid for loops and always try to use apply-type functions. But this isn’t entirely true. It may have been true for Splus, back in the day: As I recall, that had to do with the entire environment from each iteration being retained in memory. Here’s a […]

Read more »

x[[c(5,3)]]

April 3, 2013
By
x[[c(5,3)]]

An R tip: Did you know that x[[c(5,3)]] is the same as x[[5]][[3]]? I should make more thorough use of this. In the help file for [[: [[ can be applied recursively to lists, so that if the single index i is a vector of length p, alist[[i]] is equivalent to alist[[i1]]...[[ip]] providing all but […]

Read more »

Introduction to Approximate Bayesian Computation (ABC)

March 31, 2013
By
Introduction to Approximate Bayesian Computation (ABC)

Many of the posts in this blog have been concerned with using MCMC based methods for Bayesian inference. These methods are typically “exact” in the sense that they have the exact posterior distribution of interest as their target equilibrium distribution, but are obviously “approximate”, in that for any finite amount of computing time, we can […]

Read more »

as.character() for rownames()

March 22, 2012
By
as.character() for rownames()

Rainer pointed out, in response to my post, Row names in data frames: Beware of 1:nrow, that if I’d used rownames(x) <- as.character(1:3) rather than rownames(x) <- 1:3, I wouldn’t have had the problem I’d seen. If you type rownames(x) you see the same result as rownames(z), and is.character(rownames(x)) and is.character(rownames(z)) both return TRUE, but […]

Read more »

Row names in data frames: beware of 1:nrow

March 21, 2012
By
Row names in data frames: beware of 1:nrow

I spent some time puzzling over row names in data frames in R this morning. It seems that if you make the row names for a data frame, x, as 1:nrow(x), R will act as if you’d not assigned row names, and the names might get changed when you do rbind. Here’s an illustration: As […]

Read more »


Subscribe

Email:

  Subscribe