简体   繁体   English

将Zelig与ggplot2一起使用,绘制仿真和模型图

[英]Using Zelig with ggplot2, graphing simulations and models

I am attempting to using ggplot2 to graph some basic simulations and multi-variable regression models but am at a loss. 我正在尝试使用ggplot2绘制一些基本模拟和多变量回归模型的图形,但是很茫然。

I am using Zelig 3.5 (as newer Zeligs have glitches with simulations) 我正在使用Zelig 3.5 (因为更新的Zeligs在仿真方面存在故障)

Based on a blog I found, I tried this 根据我发现的博客,我尝试了

AppMod1 <- (s1$qi)
AppMod1 <- data.frame(AppMod1$ev)

AppMod1 <- melt(AppMod1,     measure=1:86)
AppMod1 <- ggplot(AppMod1, aes(approve, year)) +
                 geom_point() +
                 geom_smooth(colour="blue") +
                 theme_tufte()
AppMod1

` This didn't work. `这没有用。 I got an error 我有一个错误

"Error: measure variables not found in data:NA" “错误:未在data:NA中找到度量变量”

My models are m1, m2, and m3, and my simulations are m1 and m2. 我的模型是m1,m2和m3,而我的模拟是m1和m2。 I am using the "approval" data set which comes in Zelig. 我正在使用Zelig附带的“批准”数据集。

The models are calculated as follows 模型计算如下

data(approval)
m1 <- zelig(approve~avg.price, model="ls", data=approval)
m2 <- zelig(approve~avg.price+sept.oct.2001+iraq.war, model="ls", data=approval)
m3 <- zelig(approve~avg.price+sept.oct.2001+avg.price:sept.oct.2001, model="ls", data=approval)

And the simulations are 模拟是

x1 <- setx(m2, sept.oct.2001= 1)
s1 <- sim(m2, x=x1)
summary(s1)

x1 <- setx(m2, sept.oct.2001= 0)
s1 <- sim(m2, x=x1)
summary(s1)

oilprice <- min(approval$avg.price):max(approval$avg.price)
x2 <- setx(m2, sept.oct.2001=0, avg.price=oilprice)
s2 <-sim (m2, x=x2)
plot.ci(s2)

oilprice <- min(approval$avg.price):max(approval$avg.price)
x2 <- setx(m2, sept.oct.2001=1, avg.price=oilprice)
s2 <-sim (m2, x=x2)
plot.ci(s2)

It looks like the error resulted from your call to melt . 看来是您致电melt导致的错误。

Note that in the second line of code AppMod1 <- data.frame(AppMod1$ev) you overwrite the assignment you made in your first line of code AppMod1 <- (s1$qi) . 请注意,在代码第二行AppMod1 <- data.frame(AppMod1$ev)您将覆盖在代码第一行AppMod1 <- (s1$qi)所做的分配。 So after these two lines of code AppMod1 is equal to a data frame with the single column ev . 因此,在这两行代码之后, AppMod1等于具有单列ev的数据帧。

Now melt tries to melt this data frame and the call to melt indicates that there are 86 columns of measure.vars , when in fact there is only one column in the data frame. 现在melt尝试融合此数据帧,对melt的调用指示有86列measure.vars ,而实际上数据帧中只有一列。 That results in the error you described. 这将导致您描述的错误。

I can't quite tell from your code what you're expecting AppMod1 to look like. 我无法从您的代码中完全看出您期望AppMod1的外观。 When I run your code, s1$qi contains only NULL values. 当我运行您的代码时, s1$qi仅包含NULL值。 At the very least, you'll need AppMod1 to include columns for approve and year in order for your ggplot code to work as written. 至少,您需要AppMod1包括approveyear列,以便ggplot代码按编写的方式工作。

Hopefully this is enough information to go on for now. 希望这是足够的信息可以继续。 It will be easier to provide additional help if you show what you expect AppMod1 to look like before and after the call to melt . 如果显示melt之前和之后的期望AppMod1外观,则提供其他帮助将更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM