R is too strongly typed

I fit a multilevel model in R and called it M2, then innocently put together some coefficients in order to make a prediction:

a.hat <- fixef(M2)["(Intercept)"] + fixef(M2)["u.full"]*u + ranef(M2)$county

Then I tried

a.hat[26]

and got the following response:

Error in `[.data.frame`(a.hat, 26) : undefined columns selected

OK, OK, I had to go back and change the original line to:

a.hat <- fixef(M2)["(Intercept)"] + fixef(M2)["u.full"]*u + unlist (ranef(M2)$county)

This is a pain, because sometimes it’s “as.vector,” sometimes it’s “as.numeric,” sometimes it’s something else. It’s so hard sometimes to just access the data. R is so strongly typed now that I have to waste a lot of time simply extracting things from objects that I already have sitting in memory.

6 thoughts on “R is too strongly typed

  1. First, this isn't my field other than some programming experience.

    I think R's problem is it is to weakly typed. If you want to avoid these issues you want a strongly typed language with operator overloading.

    The core fundamental advantage of typing is that types can be interpreted by the language to do something intelligent.

    Contrast extremes. At the lowest level there are no types. You could operate on BLOBs of data. Mere pointers to memory locations with no particular language information about what is in the memory location.

    Then what? Since there is no particular information built into that language's pointers, there is nothing the language can do to do something intelligent.

    The more information there is associated with what a variable is the more intelligent the language can be about the interaction of variables which is what you want!

    You want the language to do something smart. And to do that you need strong typing.

    Stroustrup said in his book the C++ programming language that everything should be typed. He said whatever was in comments should be represented in the variable typing itself so that the comments would be unneeded. It is only in that way that the language can help you out.

  2. It's hard to tell from the code you posted what you were expecting to happen.

    R has fairly consistent rules about converting types that do the right thing in most circumstances.

    Here it looks like you've come across a situation in which the default behavior is not what you want. But R cannot "know" that one result makes more sense than another. It just uses the default behavior for an operation, and leaves it up to you to override it if it's wrong.

    R has just enough typing to make it possible to implement reasonable default operations. As Jeremiah noted, weaker typing would make it impossible to guess your intentions. Stronger typing helps to avoid insidious errors, but can make programming a bit trickier.

  3. If you're going to use the term "strongly typed" you should read the wikipedia article – "In computer science and computer programming, the term strong typing is used to describe those situations where programming languages specify one or more restrictions on how operations involving values having different datatypes can be intermixed. The antonym is weak typing. However, these terms have been given such a wide variety of meanings over the short history of computing that it is often difficult to know, out of context, what an individual writer means when using them"

    I'm also not sure whether you should be pointing the finger of blame at R, or at the output of the ranef function – it seems to be inconsistent with fixef.

  4. I think Hadley point about ranef and fixef having inconsistent results is right on. This seems to be my major complaint with R; it is a mess of language. The lack of conventions and standards in naming functions and options and in what form results are returned makes working with R far more confusing that it should. You see things like people using "." in functions and variable names that are not S3 methods. Maybe its because R people are statisticians first and programmers second that this is the case. I agree with the first guy about Python. I wish Python/Scipy were the standard.

  5. My biggest pet peeve about R:
    debugging seems to be an afterthought

    It impossible for a language without the concept a dedicated debugger to ever scale as much as R needs too. No ability to graphically set a break point in a function makes me cry at night.

Comments are closed.