How to export coefficients of the regression analysis fto a spreadsheet or csv file?

后端 未结 1 1200
猫巷女王i
猫巷女王i 2020-12-28 16:48

I am new to RStudio and I guess my question is pretty easy to solve but a lot of searching did not help me.

I am running a regression and summary(regression1)

相关标签:
1条回答
  • 2020-12-28 17:37

    There's a contributed package called broom that simplifies this task, it converts model output to tidy dataframes. Here's a self-contained reproducible example:

    Download and install the package:

    library(devtools)
    install_github("dgrtwo/broom")
    library(broom)
    

    Here's the normal base output, not very convenient:

    lmfit <- lm(mpg ~ wt, mtcars)
    lmfit
    
    Call:
    lm(formula = mpg ~ wt, data = mtcars)
    
    Coefficients:
    (Intercept)           wt  
         37.285       -5.344 
    

    Here's the same model output after it's been tidied up by the broom package, much nicer and easier to work with:

    tidy_lmfit <- tidy(lmfit)
    tidy_lmfit
             term  estimate std.error statistic      p.value
    1 (Intercept) 37.285126  1.877627 19.857575 8.241799e-19
    2          wt -5.344472  0.559101 -9.559044 1.293959e-10
    

    And here's how you'd write that dataframe to CSV:

    write.csv(tidy_lmfit, "tidy_lmfit.csv")
    
    0 讨论(0)
提交回复
热议问题