[英]Convert R code into Python code using rpy2
我是R的新手,實際上,我對R語法一無所知。 因此,我找到了rpy2包(版本2.6.1),並嘗試利用R回歸擬合函數進行數據分析。 但是,我無法使用rpy2提取回歸系數。 如果有人可以幫助告訴您如何得出回歸方程系數,例如R ^ 2,P等,真的非常感謝。
我找到了R代碼來實現此目的,但是我目前無法將其轉換為python代碼。 我找到的R代碼如下所示:
ggplotRegression <- function (fit) {
require(ggplot2)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
labs(title = paste("Adj R2 = ",signif(summary(fit)$adj.r.squared, 5),
"Intercept =",signif(fit$coef[[1]],5 ),
" Slope =",signif(fit$coef[[2]], 5),
" P =",signif(summary(fit)$coef[2,4], 5)))
}
from rpy2.robjects import r
ggplotRegression = r("""
function (fit) {
require(ggplot2)
ggplot(fit$model,
aes_string(x = names(fit$model)[2],
y = names(fit$model)[1])) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
labs(title = paste("Adj R2 = ",
signif(summary(fit)$adj.r.squared, 5),
"Intercept =",
signif(fit$coef[[1]],5 ),
" Slope =",signif(fit$coef[[2]], 5),
" P =",signif(summary(fit)$coef[2,4],
5)))
}""")
這樣,您可以在Python代碼中調用ggplotRegression函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.