相当于Stata qfit的R等价物

问题描述 投票:0回答:1

我想在R中的散点图中添加非线性回归线。我尝试过使用predict()命令,但我无法使其适用于我的数据。我的朋友在Stata做这件事,graph twoway qfit x y在那里产生了一条曲线,正如我在R中想要的那样.R中有这样的功能吗?

r statistics
1个回答
0
投票

使用library(ggplot2)回答

您想在数据中使用geom_smooth。我相信method = 'gam'会给你一条曲线。所以如果你有这个:

ggplot(data = df) + geom_scatter(aes(x = xvar, y = yvar))

你需要添加

ggplot(data = df) + geom_scatter(aes(x = xvar, y = yvar)) + 
geom_smooth(aes(x = xvar, y = yvar), method = 'gam')

有关更多信息,请参阅此处:http://ggplot2.tidyverse.org/reference/geom_smooth.html

© www.soinside.com 2019 - 2024. All rights reserved.