1

I am an R beginner (first semester - we us this programme for univariate statistics) and currently struggling with plotting the outcome of my glm(). I read quite a few threads and help files on the internet, but I have 2 problems: 1) I don't understand the advice because it is too advanced or 2) I understand the advice but when I replicate the code, it doesn't work.

I think I am close to the solution, but my curve doesn't work how it is supposed to. Can anyone tell me what I am doing wrong?

new.data<-data.frame(x=rnorm(50,0,1), y=c("yes", "no")) 
mock_model<-glm(y~x, data=new.data, family=binomial) 
x1<-seq(min(new.data$x), max(new.data$x), 0.01) 
y1<-predict(mock_model, list(x=x1), type="response") 
plot(new.data$x, new.data$y, xlab="numeric var", ylab="binary var") 
points(x1, y1)

I am new to coding and this platform, so apologies in advance if the information I have provided is not sufficient.

Any advice would be greatly appreciated.

V_A
  • 11
  • 3
  • When asking for help, you should include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data. Without some data to run the code we can't for sure see what you are seeing so it's unclear what the problem is. – MrFlick Dec 19 '17 at 18:55
  • Oh right, makes sense! Judging from the link a small data frame with variables similar to my data should be fine? `new.data<-data.frame(x=rnorm(50,0,1), y=c("yes", "no")) mock_model<-glm(y~x, data=new.data, family=binomial) x1<-seq(min(new.data$x), max(new.data$x), 0.01) y1<-predict(mock_model, list(x=x1), type="response") plot(new.data$x, new.data$y, xlab="numeric var", ylab="binary var") points(x1, y1)` – V_A Dec 19 '17 at 19:32
  • Edit this information into your original question. Comments cannot be properly formatted and it really should have been there from the start. – MrFlick Dec 19 '17 at 19:33

1 Answers1

1

Here's an example using mtcars and the ggplot2 package. The syntax of ggplot2 works roughly like this: You begin a plot with the ggplot() command, within which you can (but don't have to) define aesthetics (the aes() option), which include selection of axis variables, but can also contain options to change the visuals, like colors, linewidths etc. If you define the axis variables within ggplot(), don't forget to put the data assignment (see example below) outside of aes().

Afterwards, you add layers of geoms to plot specific things, like data points with geom_point(), lines with geom_line() or a lot of other fun things. When you want to use the variables and data assigned in the ggplot() command, just leave the geom empty (apart from any visual aes() options you want to use for that specific geom). However, you can define new data and variables for a geom, for example to use different data sources in the same plot.

data(mtcars)

model_shift <- glm(am ~ mpg, data = mtcars, family = 'binomial')
x <- seq(min(mtcars$mpg), max(mtcars$mpg), .1)
y <- predict(model_shift, list(mpg = x), type = 'response')
plot_data <- data.frame(mpg = x, am = y)

library(ggplot2)
ggplot(aes(x = mpg, y = am), data = plot_data) +
  geom_point()

enter image description here

Or with a line instead of points:

ggplot(aes(x = mpg, y = am), data = plot_data) +
  geom_line()

enter image description here

To get a glimpse of the seemingly endless possibilities of ggplot2, have a look at these 'Top 50' ggplot2 visualizations. To learn the package-specific language, see this tutorial or check your university's library for Hadley Wickham's book ggplot2: elegant graphics for data analysis.

LAP
  • 6,605
  • 2
  • 15
  • 28
  • 1
    Thanks a lot for explaining all these basics to me. When I tried with ggplot2 before it did not work for some reason, but with your step-by-step guide it did! Thank you, again. – V_A Dec 19 '17 at 19:50