I am new to R and actually, I knew nothing about R syntax. So, I found the rpy2 package(version 2.6.1) and trying to make use of the R regression fitting function for data analysis. However, I failed to extract the regression coefficients using rpy2. If anyone can help to tell how to pull out the regression equation coefficients, like R^2, P etc., Really thanks a lot.
I found out a R code to achieve this however, I cannot convert it to python code currently. The R code I have found is shown following:
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)))
}""")
With this you can call the function ggplotRegression in your Python code.
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.