如何更改点并向云图添加回归(使用 R)?
为了清楚地说明我的要求,我创建了一个简单的示例。第一步是创建一些数据:
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)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
回答您问题的前半部分,“使用云命令时如何为男性或女性做出不同的点(例如蓝色和粉红色点而不是蓝色十字)?”
这个问题的元答案可能涉及一些非 3D 可视化。也许你可以使用lattice或ggplot2将数据分割成小倍数?添加回归结果可能会更容易理解并且更容易。
默认的 splom 面板函数是 panel.pairs,您可以修改它以添加回归线,而无需大量操作的麻烦。
ggplot2 可以轻松地在绘图矩阵内进行回归,但我无法让颜色发挥作用。
最后,如果您确实想使用回归平面绘制云图,可以使用 scatterplot3d 来完成此操作包裹。请注意,我更改了数据以获得更有趣的结构:
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)?"
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.
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.
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:
使用汽车包中有很好的可视化效果="http://cran.r-project.org/web/packages/rgl/index.html" rel="nofollow noreferrer">rgl 包(openGL 实现):
There is nice fit visualization in car package using rgl package (openGL implementation):