0

I am using a regression model for some data whose explanatory variables can only be 1,2,3,4 and 5. There are 5 explanatory variables and a dependent variable. For example,

set.seed(2)
x1 <- sample(rep(1:5,2))
x2 <- sample(rep(1:5,2))
x3 <- sample(rep(1:5,2))
x4 <- sample(rep(1:5,2))
x5 <- sample(rep(1:5,2))
y <- runif(10,-1,1)

model <- lm(y~x1 + x2 + x3 + x4 + x5)

I want to create a box plot showing the relationship between these dependent variables and the dependent variable. How can I do that in R?

I have managed to create a box plot using the code provided by @Ben. However, there are some points in the plot that I do not understand. Any idea what they are for? Here is the plotenter image description here

Günal
  • 751
  • 1
  • 14
  • 29
  • Draw us an image of what you expect to get. This may help you in providing a reproducible example. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Jan 18 '13 at 13:18
  • An example has been added. However, I don't know what the output would be like. I have been asked to create a boxplot showing the relationship between these dummy variables and y. – Günal Jan 18 '13 at 13:29
  • Are your x1, x2 ,x3 really a "dummy variables" because in regression analysis "dummy variables" usually consist of values 0 and 1 but in your data they contain values 1:5. – Didzis Elferts Jan 18 '13 at 13:35
  • @didzis, you are right. I meant to say they can only be 1,2,3,4 and 5. I have edited. – Günal Jan 18 '13 at 13:38
  • It looks like your data are fairly strongly skewed, and that you have a large data set. You might try substituting `geom_violin` for `geom_boxplot` ... – Ben Bolker Jan 18 '13 at 22:13

1 Answers1

2

If you want a boxplot, you'll have to explain in much more detail what you want it to be based on. (That is, this is a programming site; you would need to say how you wanted the results of the regression to be translated into the parameters of a boxplot (central line, fences, whiskers, etc.).)

That said, you can use the coefplot function in the arm package to draw a graphical summary

library(arm)
coefplot(model)

enter image description here

Or, on second thought, maybe the model is a red herring: maybe you just want to plot the data.

d <- data.frame(y,x1,x2,x3,x4,x5)
library(reshape2)
dm <- melt(d,id.var=1)
library(ggplot2)
ggplot(dm,aes(x=value,y=y))+geom_boxplot(aes(group=value))+
    facet_wrap(~variable,nrow=1)

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Yes, the second is the response to my question because I need this plot for exploratory data analysis. Cheers @Ben :) – Günal Jan 18 '13 at 14:08
  • I will ask a questions about the plot I have generated. Therefore, I need to upload my plot here. Any idea how to do that? – Günal Jan 18 '13 at 14:55
  • You should be able to edit your question and click on the relevant button in the edit controls to upload an image file. – Ben Bolker Jan 18 '13 at 18:49
  • @Ben, in my plot there are some points. I googled this and saw that they show the outliers. Is that right? Also, how can I name the x and y axes. I tried `xlab` and `ylab`, but they do not work. Any idea? – Günal Jan 18 '13 at 19:47
  • either `+xlab("x title")+ylab("y title")`, or `+labs(x="x title",y="y title")`, right after `facet_wrap(...)`. Yes, those are "outliers", but in a very special sense: see `?boxplot.stats` – Ben Bolker Jan 18 '13 at 22:09
  • @Ben, I am wondering whether it is possible to produce a box plot for the same explanatory variables, but for a dummy response variable `y <- rbinom(10,1,0.5)`. Is this possible? If so, can you please give some clue? – Günal Jan 22 '13 at 13:05