ggplot2:如何获取回归线方程的值r ^ 2和p值? [英] ggplot2: how to get values for the regression line equation, r^2 and p value?

查看:157
本文介绍了ggplot2:如何获取回归线方程的值r ^ 2和p值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法计算出使用函数geom_smooth绘制的线性回归方程,线性回归的r ^ 2和p值.

I cant work out how to get the regression line equation, r^2 and p value of the linear regression I have plotted using the function geom_smooth.

这是我的代码:

   g <- ggplot(data=data.male, aes(x=mid_year, y=mean_tc, colour=data.male$survey_type))  
   g <- g + geom_point(shape = 20, size =2) 
   g <- g + geom_smooth(method=lm, na.rm = FALSE, se = TRUE, aes(group=1), colour = "black")
   g <- g + theme_gray(base_size=24)
   g <- g+ xlab("Year")
   g <- g + ylab("Mean serum total cholesterol (mmol/L)")
   g <- g + theme(legend.position="bottom")
   g <- g + scale_y_continuous(limits=c(3.5,6.5), breaks=c(3.5,4,4.5,5,5.5,6,6.5))
   g <- g + scale_x_continuous(limits=c(1980,2015), breaks=c(1980,1990,2000,2010))
   g <- g + scale_colour_manual(name = "Survey Type", values= c("Red", "Blue", "Green")) 
   g  

[1]:

推荐答案

请勿使用绘图功能进行建模.使用 lm 函数拟合模型.

Don't use a plotting function for modelling. Fit the model using the lm function.

然后使用 summary 方法获取有关适合度的所有信息.

Then use the summary method to get everything you need to know about the fit.

您应该获得与绘图功能相同的结果,我怀疑在内部使用 lm .

You should get the same results as the plotting function, which I suspect uses lm internally.

这篇关于ggplot2:如何获取回归线方程的值r ^ 2和p值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆