Hello,
I've found some examples performing loops with linear regression over one variable.
They use group_map and broom. That's fine.
But I can't find info about using map2 or group_map2 (if it exists) in order to move 2 variables /list at the moment of regress...
library(gapminder)
data<-gapminder
data
explic<-list("year","gdpPercap")
data %>%
group_by(country) %>%
group_modify(~broom::glance(lm(lifeExp~explic,data=.x)))
The code fails. If I replace explic in the broom line by a variable, as year, It works computing each regression for country.
It seems strange the example, but I read examples looping over one variable with regress and map.
As always, thanks for your time and interest.
Have a nice day.
Thanks for your code.
But I have a doubt with It.
In case you are working with a survey, using pivot longer can modify some statistics.
Statistics as the variance and related.
That's why I need to perform something as map2 over a list of variables.
Across also would help me in doing the task but preserving the data structure.
I collapse trying It...
R functions do not perform in-place modifications so the original data frame structure is safe, and regarding the linear model fitting, it has no effect either since this is applied over nested data anyways, so it would be equivalent to manually selecting the independent variable one by one.
across() is not meant to be used within the formula argument. I don't see a way to perform this in a wide format, and also, how would you integrate the glance() output within the original data frame structure? There is no sensible way that I can imagine.
I can't vouch for the validity of your approach from a statistical standpoint, but, to test my code you only need to manually filter by any country and perform a linear regression, the results are exactly the same.
I forgot something important.
The packages srvyr or survey don't work with pivot wider...
I think that's related to the variance treatment and alike.
I know your code works flawless under any other circumstance, andresrcs.
But dealing with surveys is a bit different...