如何更改点并向云图添加回归(使用 R)? [英] How to change points and add a regression to a cloudplot (using R)?
问题描述
为了明确我的要求,我创建了一个简单的示例.第一步是创建一些数据:
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)
所以这些是合并到一个 data.frame
的一些随机日期.所以从这些日期开始,我想绘制一个云,在那里我可以区分男性和女性,并在其中添加两个简单的回归(一个用于女性,一个用于男性).所以我已经开始了,但我无法达到我想要的地步.请看下面我到目前为止所做的:
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)
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!
推荐答案
回答你前半段的问题,使用云命令时如何为男性或女性制作不同的点(例如蓝色和粉色的点insted只是蓝色十字)?"
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 )
对此的元答案可能涉及一些非 3d 可视化.也许您可以使用lattice 或ggplot2 将数据拆分为小倍数?添加回归结果可能会更容易理解,也可能更容易.
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 面板函数是 panel.pairs,您可以修改它以添加回归线,而不会带来很多麻烦.
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 很容易在绘图矩阵内进行回归,但我无法让颜色起作用.
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")
最后,如果你真的想用回归平面做一个云图,这里有一种使用 scatterplot3d 包的方法.请注意,我更改了数据以查看更有趣的结构:
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)
这篇关于如何更改点并向云图添加回归(使用 R)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!