I have continued playing with the tidyverse for different parts of a couple of projects. Often I need to apply a function by groups of observations; sometimes, that function returns more than a single number. It could be something like for each group fit a distribution and return the distribution parameters. Or, simpler for the… Continue reading Functions with multiple results in tidyverse
Category: programming
Turtles all the way down
One of the main uses for R is for exploration and learning. Let's say that I wanted to learn simple linear regression (the bread and butter of statistics) and see how the formulas work. I could simulate a simple example and fit the regression with R:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
library(arm) # For display() # Simulate 5 observations set.seed(50) x <- 1:5 y <- 2 + 3*x + rnorm(5, mean = 0, sd = 3) # Fit regression reg <- lm(y ~ x, dat) display(reg) # lm(formula = y ~ x, data = dat) # coef.est coef.se # (Intercept) 3.99 3.05 # x 2.04 0.92 # --- # n = 5, k = 2 # residual sd = 2.91, R-Squared = 0.62 # Plot it plot(y ~ x) abline(coef(reg)) |
The formulas for the intercept ($latex b_0$) and… Continue reading Turtles all the way down
Old dog and the tidyverse
I started using R ages ago and have happily lived in mostly-base-R for data manipulation. Once in a while I move to something that makes a big difference, like ggplot2 in 2010 or Rmarkdown in 2015, but the set of packages I use for data + plotting hasn't seen many changes. I have to confess… Continue reading Old dog and the tidyverse
Left-to-right
When I write code I'm often amazed by the direction of the statements. I mean, we read and write left-to-right except when we assign statements to a variable. So here we are, doing our business, slowly working in a sentence and, puff!, we get this insight from the future and we assign it to our… Continue reading Left-to-right
Sometimes I feel (some) need for speed
I'm the first to acknowledge that most of my code could run faster. The truth of the matter is that, in essence, I write 'quickies': code that will run once or twice, so there is no incentive to spend days or hours in shaving seconds of a computation. Most analyses of research data fall in… Continue reading Sometimes I feel (some) need for speed