I am using rpy2 to access a function which accepts the variables and then predicts the output for a GAM Model .
However, I am facing the following error during python runtime:
RRuntimeError Traceback (most recent call last)
<ipython-input-12-e571fb4e8082> in <module>
----> 1 gam_model(0.1, 13000, 1300, .5, 9000, 4, 10)
{PATH_TO_ENV}\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
176 v = kwargs.pop(k)
177 kwargs[r_k] = v
--> 178 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
179
180 pattern_link = re.compile(r'\\link\{(.+?)\}')
{PATH_TO_ENV}\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)
104 for k, v in kwargs.items():
105 new_kwargs[k] = conversion.py2ri(v)
--> 106 res = super(Function, self).__call__(*new_args, **new_kwargs)
107 res = conversion.ri2ro(res)
108 return res
RRuntimeError: Error in qr.lm(object) : lm object does not have a proper 'qr' component.
Rank zero or should not have used lm(.., qr=FALSE)
There does not seem to be a problem in in the R side as I tried the script alone and was able to run the function as expected. Following is the code for the R function and the Python call respectively. Also I tested the integration on a simple Linear regression model with one variable and it was working fine as well.
The R function:
gam_model <- function(var1, var2,
var3, var4, var5,
var6, var7, ...){
x <- data.frame(var1=var1, var2=var2,
var3=var3, var4=var4,
var5=var5, var6=var6, var7=var7)
model <- readRDS("models/gam_1.rds")
result <- predict(model, x)
return(result)
}
The python call:
from rpy2 import robjects
from rpy2.robjects import pandas2ri
r = robjects.r
r['source']('model_functions.R')
gam_model = robjects.globalenv['gam_model']
y_pred = gam_model(0.1, 13000, 1300, .5, 9000, 4, 10)
I have identified the issue as the package 'mgcv' is not installed. Installing will resolve the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.