-2

I am trying to create a histogram of a variable with superimposed the lines of the 2 gammas distribution and the mixture of those two. But when I insert the following code:

        hist(age, breaks = 50,freq = FALSE)
    lines(seq(min(age),max(age),length=length(age)),age.GA.2[["prob"]][1]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat1, sigma = sigma.hat1),lty=2,lwd=3,col=2) 
    lines(seq(min(age),max(age),length=length(age),age.GA.2[["prob"]][2]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat2, sigma = sigma.hat2),lty=2,lwd=3,col=3) 
    lines(seq(min(age),max(age),length=length(age)),
    age.GA.2[["prob"]][1]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat1, sigma = sigma.hat1) +
    age.GA.2[["prob"]][2]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat2, sigma = sigma.hat2),
    lty = 1, lwd = 3, col = 1)

I got this error result:

> hist(age, breaks = 50,freq = FALSE)
> lines(seq(min(age),max(age),length=length(age)),age.GA.2[["prob"]][1]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat1, sigma = sigma.hat1),lty=2,lwd=3,col=2) 
> lines(seq(min(age),max(age),length=length(age),age.GA.2[["prob"]][2]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat2, sigma = sigma.hat2),lty=2,lwd=3,col=3) 
+ lines(seq(min(age),max(age),length=length(age)),
Errore: unexpected symbol in:
"lines(seq(min(age),max(age),length=length(age),age.GA.2[["prob"]][2]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat2, sigma = sigma.hat2),lty=2,lwd=3,col=3) 
lines"
> age.GA.2[["prob"]][1]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat1, sigma = sigma.hat1) +
+ age.GA.2[["prob"]][2]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat2, sigma = sigma.hat2), 
Errore: unexpected ',' in:
"age.GA.2[["prob"]][1]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat1, sigma = sigma.hat1) +
age.GA.2[["prob"]][2]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat2, sigma = sigma.hat2),"
> lty = 1, lwd = 3, col = 1)
Errore: unexpected ',' in "lty = 1,"

I still haven't figured out what is the problem. I took the exact code from a file given us by the professor, I just changed the values for the database and the fits.

LJS
  • 11
  • 2
  • Can you post sample data? Please edit **the question** with the output of `dput(age)`. Or, if it is too big with the output of `dput(head(age, 20))`. – Rui Barradas Feb 05 '21 at 16:39

1 Answers1

0

In the third line of your code, you are missing a ) after lines(seq(min(age),max(age),length=length(age) to close the seq() function. Modify the line like this:

lines(seq(min(age),max(age),length=length(age)),age.GA.2[["prob"]][2]*dGA(seq(min(age),max(age),length=length(age)), mu = mu.hat2, sigma = sigma.hat2),lty=2,lwd=3,col=3) 
pascal
  • 1,036
  • 5
  • 15