'capturing the residual from felm in original dataframe and passing to Python
I am running R via Python using %load_ext rpy2.ipython.
#Prepare R in Python
%load_ext rpy2.ipython
%R install.packages("pacman")
%R pacman::p_load(pacman, tidyverse, lfe)
In R, I am running a model using felm on a dataframe. I want to capture the residuals in the original dataframe and then pass that dataframe back into Python. I can do this for regular lm as:
%%R -o test
dat <- mtcars
test <- mutate(res=residuals(lm(mpg~disp, data = dat)), dat)
Then in Python I have a nice dataframe with res included:
test.describe()
Unfortunately, this approach doesn't seem to work when I use felm instead of lm:
%%R -o test2
dat <- mtcars
test2 <- mutate(res=residuals(felm(mpg~disp, data = dat)), dat)
I get the following: "ValueError: Data must be 1-dimensional" Any ideas why this works for lm and not for felm? I did notice that when I run:
%%R
dat <- mtcars
test2 <- mutate(res=residuals(felm(mpg~disp, data = dat)), dat)
head(test2, 10)
the residual is calculated correctly and appears to be added to the dataframe but is named "mpg" instead of "res." Not sure what is going on here. Any help is appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
