简体   繁体   中英

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.

I am using Zelig 3.5 (as newer Zeligs have glitches with simulations)

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"

My models are m1, m2, and m3, and my simulations are m1 and m2. I am using the "approval" data set which comes in 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 .

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) . So after these two lines of code AppMod1 is equal to a data frame with the single column 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. That results in the error you described.

I can't quite tell from your code what you're expecting AppMod1 to look like. When I run your code, s1$qi contains only NULL values. 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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