Ошибка : Преобразование 'rpy2py' не определено для объектов типа
Я использую библиотеку python R для реализации модели ERGM для набора данных. Я следую приведенному ниже подходу
Когда я реализую этот код, я получаю ошибку Conversion 'rpy2py' not defined for objects of type error
Я попытался реализовать следующий метод
Но когда я подхожу к нему, он выдает ошибку, связанную с fit_summary$se, говоря, что аргументы равны 4,0
Maximum Pseudolikelihood Results:
Estimate Std. Error MCMC % z value Pr(>|z|)
edges -11.728186 1.263969 0 -9.279 <1e-04 ***
triangle 2.088686 0.108218 0 19.301 <1e-04 ***
mutual 8.638105 0.817793 0 10.563 <1e-04 ***
nodecov.Attendance -0.034097 0.007857 0 -4.339 <1e-04 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Warning: The standard errors are based on naive pseudolikelihood and are suspect. Set control.ergm$MPLE.covariance.method='Godambe' for a simulation-based approximation of the standard errors.
Null Pseudo-deviance: 1.469e+09 on 1.059e+09 degrees of freedom
Residual Pseudo-deviance: 1.051e+03 on 1.059e+09 degrees of freedom
AIC: 1059 BIC: 1134 (Smaller is better. MC Std. Err. = 0)
# Adjust the R script for better compatibility and error handling
ro.r('''
library(ergm)
net <- network::network(matrix(edges, byrow=TRUE, ncol=2), directed=TRUE, vertices=node_ids)
for (attr_name in colnames(nodes)) {
set.vertex.attribute(net, attr_name, nodes[[attr_name]])
}
fit <- ergm(net ~ edges + triangle + mutual + nodecov('Attendance'), estimate = "MPLE")
# Check if fit is valid and prepare summary
if (!is.null(fit)) {
fit_summary <- summary(fit) # This should be the raw summary output
if (!is.null(fit_summary)) {
fit_df <- data.frame(
Estimate = fit_summary$coefficients,
Std.Error = fit_summary$se,
Z.value = fit_summary$coefficients / fit_summary$se,
P.value = fit_summary$p.values
)
print(fit_df)
.GlobalEnv$fit_df <- fit_df # Save DataFrame to global environment
} else {
print("Fit summary is NULL or not available.")
}
} else {
print("ERGM model did not fit properly.")
}
''')
if 'fit_df' in ro.globalenv and not r['is.null'](ro.globalenv['fit_df']):
summary_df = pandas2ri.rpy2py(ro.globalenv['fit_df'])
results = {
"Estimates": summary_df["Estimate"].tolist(),
"Std. Errors": summary_df["Std.Error"].tolist(),
"Z-values": summary_df["Z.value"].tolist(),
"P-values": summary_df["P.value"].tolist(),
}
return JsonResponse(results)
else:
logger.error("Model summary is NULL or the DataFrame could not be created.")
return JsonResponse({"error": "Model did not converge or insufficient data for summary"}, status=500)
except Exception as e:
logger.exception("Error during ERGM process: %s", str(e))
return JsonResponse({"error": str(e)}, status=500)