1

I'm working on a for function that uses the input data to generate several linear regression models, a~c. This data generate works perfectly fine, but, I need to export the data resulting to a csv file. I'm using the tydy() and glance () functions to obtain p values, intercepts, r2 etc. That part of the code works fine, but, the output file does not provide me with the "Call Formula:" of the linear regresion, so I'm having problem to interpret the out... Can someone tell me, please, how to make the call formula become the header of the csv file?.

G5W
  • 36,531
  • 10
  • 47
  • 80
  • 1
    Try to save the formula as a character/string variable in a new column in your data frame and then save it as a csv file. Try this simple example `library(broom); frml = 'disp ~ hp + cyl'; m = lm(frml, data = mtcars); dt_m = tidy(m); dt_m$frml = frml; dt_m` – AntoniosK Jan 29 '18 at 18:01
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 29 '18 at 18:40

1 Answers1

0

I don't think that csv files have a singular header that you can save the call to but if you simply want to capture the call to lm with the results then (using mtcars data since you didn't provide much context):

library(broom)
m = lm(disp ~ hp + cyl, data = mtcars)
xxx<-tidy(m)
xxx$call<-toString(m$call)
xxx

produces...

         term     estimate  std.error  statistic      p.value                        call
1 (Intercept) -144.5694333 37.6522356 -3.8395976 6.173165e-04 lm, disp ~ hp + cyl, mtcars
2          hp    0.2358181  0.2578106  0.9146953 3.678948e-01 lm, disp ~ hp + cyl, mtcars
3         cyl   55.0625843  9.8975401  5.5632595 5.310020e-06 lm, disp ~ hp + cyl, mtcars
Chuck P
  • 3,862
  • 3
  • 9
  • 20