0

I have the following data obtained from a survey among women. I ran some commands and obtained the coefficients, p value and relative risk as I followed the guidelines displayed by [http://www.ats.ucla.edu/stat/r/dae/mlogit.htm] I appreciate them.

I came with the following commands

However, I could not get graphs displayed.

Can someone help me out please? Thanks.

Here is the data

age:32 39 40 35 55 30 60 55 28 25 35 28 30 50 25 35 47 38 54 38 54 48 52 48 41 51 43 49 51 63 39 64 38 29 36 44 57 28 46 51 22 60 56 55 41 35 30 60 68 30 36 25 45 50 32 43 52 55 45 58 53 51 50 52 80 67 48 38 32 52 35 60 51 34 46 38 35 28 80 48 91 40 45 40 58 40 45 37 38 25 58 53 42 60 70 68 62 53 53 66 40 39 35 32 25 40 40 45 51 48 58 44 47 52 57 35 57 92 88 87 43 44 40 47 41 52 40 42 49 40 37 40 38 30 44 38 39 32 41 38 42 40 29 26 38 31 43 38 36 32 28 28 34 37 32 40 39 31 42 38 38 36

rel:Chris Chris Chris Chris Trad Islam Trad Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Islam Chris Chris Chris Chris Chris Islam Chris Trad Chris Chris Islam Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Islam Chris Chris Chris Chris Chris Chris Chris Chris Chris Islam Chris Chris Chris Chris Chris Islam Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Trad Chris Chris Chris Chris Trad Chris Chris Chris Chris Chris Chris Chris Chris Trad Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Chris Trad Trad Chris Chris Chris Chris Chris

pet:Three Three Two Three Three One Two One Three One Two Three Three Three Two Three Three Three Three Three Three Three One Three Three Three Three Three Three Two Two Two Three Three Three Two Three Three Three Three One One Three Three Three One Three Three One One Three Three One One Three Three Three Three Two Three Three Three Three Two One One Two Two One Two One One Two One One One Two One Three One Two One Two Two Three One Two Two Two Two Two Two Three Two Two Two Two Two One One Three One Three Three Three Three One Three Three Two Three Three Three Three Three One Three Two One Two Three Three Three Two Two One Three Two Three Two Three Two Three Two Three Three Three Three Two Three Two Three Three Two Two Three Three Two Three Three Three Three Three Three Three Three Two Three Three Three Three Three

Sec=read.table("Second.txt", header=TRUE)
Sec
attach(Sec)    
library(nnet)
library(ggplot2)
library(reshape2)
m1 <- multinom(pet ~ age+rel, data = Sec)
summary(m1)
Relrisk=exp(coef(m1))
Relrisk
z <- summary(m1)$coefficients/summary(m1)$standard.errors
z
p <- (1 - pnorm(abs(z), 0, 1))*2
p
 frame=data.frame(age=rep(seq(22,92,by=0.434),3),rel=rep(c("Chris","Islam","Trad"),each=54))
frame
attach(frame)
pred=cbind(frame,predict(m1,newdata=frame,type="probs", se= TRUE))
pred

Preprobmelt=melt(pred,id.vars=c("rel","age"),value.name="Probability")
Preprobmelt

Graph=ggplot(Preprobmelt,aes(x = age,y=Probability, colour=rel,group=age))

geom_line()

facet_grid(variable~ .,scales = "free")

Graph

No lines displayed

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • 1
    Here are a few tips for the [next time](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) you want to post a well formed question. – Roman Luštrik Nov 22 '16 at 18:06

2 Answers2

2

I don't see the necessary plus signs for constructing a ggplot object properly. It should be:

 Graph=ggplot(Preprobmelt,aes(x = age,y=Probability, colour=rel,group=age)) + 
  geom_line() + 
  facet_grid(variable~ .,scales = "free")

Note carefully how three things are being added together with plus (+) signs.

You should probably also explicitly print ggplot objects in a script:

 print(Graph)
Spacedman
  • 92,590
  • 12
  • 140
  • 224
0

Just try this (you don't need the continuous variable 'age' as group in ggplot aes()):

Graph=ggplot(Preprobmelt,aes(x = age,y=Probability, colour=rel)) + 
geom_line(lwd=1.2) +
facet_grid(variable~ .,scales = "free")

Graph

enter image description here

Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63