1

I am running fifteen ppml regressions using a for loop (the code below): (d is the list of my dependent variable.) ppml is from gravity package.

for (i in 1:15) { 
   fit <- ppml(
    dependent_variable = d[i],
    distance = "y",
    additional_regressors = c("x1", "x2" , "x3"),
    robust = FALSE,
    data = mydata
  )
   print(d[i])
   print(fit$coefficients[1:7])
   #print(summary(fit))
}

The output of ppml is strange as it is not just one class:

> class(fit)
[1] "glm"  "lm"   "ppml"

I want to create 15 data-frames and save each of the results in one data-frame.

Any suggestions?

Naim
  • 41
  • 1
  • 1
  • 9
  • check out the `assign` function – morgan121 Feb 12 '19 at 23:53
  • 4
    I'd strongly advise against using `assign`, see [Why is using assign bad?](https://stackoverflow.com/questions/17559390/why-is-using-assign-bad); instead, why not use `lapply` to store results from every fit in a `list`. Something along the lines of `res <- lapply(d, function(x) ppml(dependent_variable = x, distance = "y", ...))`; it's then easy to operate on `res` with `lapply` to extract coefficients and tidy up the output according to your requirements. – Maurits Evers Feb 13 '19 at 00:12
  • 1
    I'll second the notion against `assign`. Not that RAB is wrong -- it would certainly work -- but it is often poor form (subjective) with regards to side-effect and data management. When I have multiple frames/arrays of data that are all similar-enough structure that when I do something to one of them, I do it to all (most) of them, then I typically put them in a `list` (better: read them into a `list` the first time) and then "iterate" over them using `lapply` (or `sapply`, depending). In R, it is a much more efficient way of handling multiple like-shaped objects. – r2evans Feb 13 '19 at 00:17
  • Thank you! `lapply` worked as well as `assign`. I agree `lappy` is both faster and more manageable. – Naim Feb 13 '19 at 03:29

0 Answers0