如何更改点并向云图添加回归(使用 R)?

发布于 2024-11-25 04:38:09 字数 967 浏览 2 评论 0原文

为了清楚地说明我的要求,我创建了一个简单的示例。第一步是创建一些数据:

gender <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2),labels = c("male", "female"))
numberofdrugs <- rpois(84, 50) + 1
geneticvalue <- rpois(84,75)
death <- rpois(42,50) + 15
y <- data.frame(death, numberofdrugs, geneticvalue, gender)

这些是合并到一个 data.frame 中的一些随机日期。因此,从这些日期开始,我想绘制一个云,在其中可以区分男性和女性,并添加两个简单的回归(一个用于女性,一个用于男性)。所以我已经开始了,但我无法达到我想要的程度。请参阅下面我到目前为止所做的事情:

require(lattice)
cloud(y$death~y$numberofdrugs*geneticvalue)

基本形式的云图

xmale <- subset(y, gender=="male")
xfemale <- subset(y, gender=="female")

death.lm.male <- lm(death~numberofdrugs+geneticvalue, data=xmale)
death.lm.female <- lm(death~numberofdrugs+geneticvalue, data=xfemale)

我如何为男性或女性提出不同的观点使用云命令时(例如蓝色和粉红色点而不仅仅是蓝色十字),如何将两个估计模型添加到云图中?

任何想法表示赞赏!感谢您的想法!

To make clear what I'm asking I've created an easy example. Step one is to create some data:

gender <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2),labels = c("male", "female"))
numberofdrugs <- rpois(84, 50) + 1
geneticvalue <- rpois(84,75)
death <- rpois(42,50) + 15
y <- data.frame(death, numberofdrugs, geneticvalue, gender)

So these are some random dates merged to one data.frame. So from these dates I'd like to plot a cloud where I can differ between the males and females and where I add two simple regressions (one for females and one for males). So I've started, but I couldn't get to the point where I want to be. Please see below what I've done so far:

require(lattice)
cloud(y$death~y$numberofdrugs*geneticvalue)

cloud plot in basic form

xmale <- subset(y, gender=="male")
xfemale <- subset(y, gender=="female")

death.lm.male <- lm(death~numberofdrugs+geneticvalue, data=xmale)
death.lm.female <- lm(death~numberofdrugs+geneticvalue, data=xfemale)

How can I make different points for males or females when using the cloud command (for example blue and pink points instead of just blue crosses) and how can I add the two estimated models to the cloud graph?

Any thought is appreciated! Thanks for your ideas!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清秋悲枫 2024-12-02 04:38:09

回答您问题的前半部分,“使用云命令时如何为男性或女性做出不同的点(例如蓝色和粉红色点而不是蓝色十字)?”

 cloud( death ~ numberofdrugs*geneticvalue , groups=gender, data=y )

分组云图

这个问题的元答案可能涉及一些非 3D 可视化。也许你可以使用lattice或ggplot2将数据分割成小倍数?添加回归结果可能会更容易理解并且更容易。

splom( ~ data.frame( death, numberofdrugs, geneticvalue ), groups=gender, data=y )

splom

默认的 splom 面板函数是 panel.pairs,您可以修改它以添加回归线,而无需大量操作的麻烦。

ggplot2 可以轻松地在绘图矩阵内进行回归,但我无法让颜色发挥作用。

pm <- plotmatrix( y[ , 1:3], mapping = aes(color=death) )
pm + geom_smooth(method="lm")

plotmatrix

最后,如果您确实想使用回归平面绘制云图,可以使用 scatterplot3d 来完成此操作包裹。请注意,我更改了数据以获得更有趣的结构:

numberofdrugs <- rpois( 84, 50 ) + 1
geneticvalue <- numberofdrugs + rpois( 84, 75 )
death <- geneticvalue + rpois( 42, 50 ) + 15
y <- data.frame( death, numberofdrugs, geneticvalue, gender )

library(scatterplot3d) 
pts <- as.numeric( as.factor(y$gender) ) + 4
s <-scatterplot3d( y$death, y$numberofdrugs, y$geneticvalue, pch=pts, type="p", highlight.3d=TRUE )
fit <- lm( y$death ~ y$numberofdrugs + y$geneticvalue )
s$plane3d(fit)

scatterplot3d with regressionplane

Answer to the first half of your question, "How can I make different points for males or females when using the cloud command (for example blue and pink points insted of just blue crosses)?"

 cloud( death ~ numberofdrugs*geneticvalue , groups=gender, data=y )

grouped cloud plot

The meta-answer to this may involve some non-3d visualization. Perhaps you can use lattice or ggplot2 to split the data into small multiples? It will likely be more comprehensible and likely easier to add the regression results.

splom( ~ data.frame( death, numberofdrugs, geneticvalue ), groups=gender, data=y )

splom

The default splom panel function is panel.pairs, and you could likely modify it to add a regression line without an enormous amount of trouble.

ggplot2 does regressions within the plot matrix easily, but I can't get the colors to work.

pm <- plotmatrix( y[ , 1:3], mapping = aes(color=death) )
pm + geom_smooth(method="lm")

plotmatrix

And finally, if you really want to do a cloudplot with a regression plane, here's a way to do it using the scatterplot3d package. Note I changed the data to have a little more interesting structure to see:

numberofdrugs <- rpois( 84, 50 ) + 1
geneticvalue <- numberofdrugs + rpois( 84, 75 )
death <- geneticvalue + rpois( 42, 50 ) + 15
y <- data.frame( death, numberofdrugs, geneticvalue, gender )

library(scatterplot3d) 
pts <- as.numeric( as.factor(y$gender) ) + 4
s <-scatterplot3d( y$death, y$numberofdrugs, y$geneticvalue, pch=pts, type="p", highlight.3d=TRUE )
fit <- lm( y$death ~ y$numberofdrugs + y$geneticvalue )
s$plane3d(fit)

scatterplot3d with regression plane

古镇旧梦 2024-12-02 04:38:09

使用汽车包中有很好的可视化效果="http://cran.r-project.org/web/packages/rgl/index.html" rel="nofollow noreferrer">rgl 包(openGL 实现):

require(car)
require(rgl)
scatter3d(death~numberofdrugs+geneticvalue, groups=y$gender, data=y, parallel=FALSE)

3d 拟合汽车包

There is nice fit visualization in car package using rgl package (openGL implementation):

require(car)
require(rgl)
scatter3d(death~numberofdrugs+geneticvalue, groups=y$gender, data=y, parallel=FALSE)

3d fit with car package

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文