-4

I ran several time-series regressions (one for every year) and now I'd like to generate a table similar to what coef() returns but also with level of significance (the stars), R-squared and F-statistic for each year which will look somewhat like this:

        b0    b1    b2    b3    b4    R-sq.    F-stat.
2010    ...*   
2011          ...
2012                ...**

So far I tried mtable() from the memisc-package which gives me years as columns and coeffecients as rows but I'd prefer the result to be "transposed" (like above).

  • 1
    What have you tried so far? – duckmayr Jan 19 '19 at 15:25
  • I edited my question. My experience with R is basically zero. Sorry about that. – procrastinator Jan 19 '19 at 16:40
  • It's OK, welcome to Stack Overflow! It just helps others help you to know what you've tried so far and where you had issues. Have you tried `t(mtable())`? (Disclaimer: I don't know if that will work, but it's my knee-jerk response). On a related note, some sample data helps answerers by providing a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – duckmayr Jan 19 '19 at 17:11
  • Trying `t(mtable()` I get "Error in FUN(X[[i]], ...) : subscript out of bounds" – procrastinator Jan 19 '19 at 18:39

1 Answers1

0

Since we don't have access to your data or the code you used to run your models, I created my own dummy models using the mtcars dataset:

data("mtcars")
model1 <- lm(mpg ~ wt + cyl, data = mtcars)
model2 <- lm(mpg ~ wt + cyl + hp, data = mtcars)

For future reference, you'll always want to supply some of your data using, for example, dput(head(my_dataframe, 20)). You should also put up more of the code you used to get where you're at; in fact, the minimum amount of code needed to reproduce your problem. You may want to read How to Create a Great R Reproducible Example for more information; it just helps others help you.

Then I rigged up the following (clumsy) function that I think does roughly what you're looking for. In any event, it should get you started in the right direction:

get_row <- function(x, coef_names) {
    coef_mat <- coef(summary(x))
    these_coef_names <- rownames(coef_mat)
    rows <- match(coef_names, these_coef_names)
    p <- coef_mat[rows, 4]
    stars <- c("", "*", "**", "***")[(p < 0.05) + (p < 0.01) + (p < 0.001) + 1]
    coefs <- round(coef_mat[rows, 1], 3)
    output <- paste0(coefs, stars)
    output <- ifelse(grepl("NA", output), NA, output)
    return(output)
}
get_table <- function(...) {
    models <- list(...)
    if ( any(lapply(models, class) != "lm" ) ) {
        stop("This function has only been tested with lm objects.")
    }
    coef_names <- unique(unlist(sapply(models, variable.names)))
    coef_table <- t(sapply(models, get_row, coef_names))
    colnames(coef_table) <- coef_names
    return(coef_table)
}

get_table(model1, model2)

#      (Intercept) wt          cyl        hp      
# [1,] "39.686***" "-3.191***" "-1.508**" NA      
# [2,] "38.752***" "-3.167***" "-0.942"   "-0.018"
duckmayr
  • 16,303
  • 3
  • 35
  • 53