Skip to content

plotting average effects.

7 messages · poliscigradstudent, Bart Joosen, gradstudent +1 more

#
hi...  i am a phd student using r.  i am having difficulty plotting average
effects.  admittedly, i am not really understanding what each of the
commands mean so when i get the error i am not sure where the issue is.

here is my code...  i will include the points at which there are errors....
+ 	ylab="Predicted Probability", axes=F)
+ 	ylab="Predicted Probability", axes=F)
Error in xy.coords(x, y) : 'x' and 'y' lengths differ


as i understand it, i need to change the means[,1]....  i have tried a few
combos and i am not getting anywhere...  

further, my arrows are huge and points are not appearing in my plot.

is there anywhere i can find a break down of each of these commands and what
each part means?  i understand the lengths, colors, xlab, ylab, etc etc.

thanks in advance for any insight you can give me.


http://r.789695.n4.nabble.com/file/n3923982/effplot_copy.jpg 

--
View this message in context: http://r.789695.n4.nabble.com/plotting-average-effects-tp3923982p3923982.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi Denins, and thanks for your reply.

I understand x,y are not lining up.  I just don't know how to fix it in the
code.

There is only a small group of us at my university using R (4 people of
which I am one).  2 are not even touching the average effects plot option,
however myself and my study partner feel it is best.  So, we really don't
have anyone to ask.  Everyone else in our class was taught on STATA.  The
reasons why are sort of complicated and I don't wish to bore you with
details. Basically, we were the first group to be trained using R.  This is
our 3rd semester using it.

Is there an online guide anywhere that will describe exactly what is going
on in the plot function for average effects?  I have been googling and have
not come across anything useful, except this site.

-----
Ph.D. Candidate
--
View this message in context: http://r.789695.n4.nabble.com/plotting-average-effects-tp3923982p3925293.html
Sent from the R help mailing list archive at Nabble.com.
#
Bart,

I apologize.  I posted the code I was using in my first comment, to include
the error and the plot that is coming up.  I was unaware that was not
enough.  I am not looking for anyone to give me  the actual answer to my
specific issue, only looking to be pointed in a direction for an online
guide that i can read to understand how average effects are plotted in R. 
Our text book doesn't cover any topics for using R, only theory.

-----
Ph.D. Candidate
--
View this message in context: http://r.789695.n4.nabble.com/plotting-average-effects-tp3923982p3925350.html
Sent from the R help mailing list archive at Nabble.com.
#
i will include the data to read if if you so choose.

dat <- read.dta("http://quantoid.net/hw1_2011.dta")

model in question:

mod99 <- glm(democracy ~ popc100kpc + ngrevpc, data=dat, family=binomial)
------------------

looking for average effects code, with error on mod99.  popckpc is coded in
1k per capita.
----
breaking the variable into groups:
+ 	apply, 2, mean)
----
and finally attempting to plot:
+ 	ylab="Predicted Probability", axes=F)
Error in xy.coords(x, y) : 'x' and 'y' lengths differ
Error in axis(1, at = 1:10, labels = rownames(means), las = 2) : 
  'at' and 'labels' lengths differ, 10 != 4
--------


I am not sure how to fix the error.  Thank you for your time.




-----
Ph.D. Candidate
--
View this message in context: http://r.789695.n4.nabble.com/plotting-average-effects-tp3923982p3925945.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi:

Your approach to computing the means is not efficient; a better way
would be to use the aggregate() function. I would start by combining
the grouping variable and the three prediction variables into a data
frame. To get the groupwise mean for all three prediction variables,
you can use a formula interface for aggregate() if you have R-2.11.0
or later, cbinding the three prediction variables into a matrix on the
LHS of the model formula, the grouping variable on the RHS, followed
by the data frame name and the summary function. See ?aggregate for
details; in particular, study the examples with a formula interface.
Save the result to an object. Since this is homework, the details are
left to you.

As far as the base graphics plot goes, I suggest the following:
  - use arrows() to produce the lines with arrows
  - plot the means by group as points with the points() function.

The arrows() function can take vector arguments; read its help page carefully.

The ggplot2 version of the plot I think you're trying to generate is
given below:

library('ggplot2')
ggplot(pmeans) +
   geom_point(aes(x = grp, y = pred1), colour = 'red') +
   geom_segment(aes(x = grp, xend = grp, y = pred3, yend = pred2),
                arrow = arrow(length = unit(0.4, 'cm')), colour =
'red', size = 1)

pmeans is the name I gave for the averaged predictions by group, with
grp representing the grouping variable and pred1-pred3 per your
definitions.

In addition to the aggregate() and apply family functions, the
packages doBy, plyr and data.table are well designed for groupwise
data summarization and processing.

HTH,
Dennis
On Fri, Oct 21, 2011 at 8:51 AM, gradstudent <nmford at uwm.edu> wrote: