Recreate minitab normal probability plot

前端 未结 4 1489
小鲜肉
小鲜肉 2021-02-06 12:30

I am trying to recreate the following plot with R. Minitab describes this as a normal probability plot.

\"alt

4条回答
  •  生来不讨喜
    2021-02-06 13:14

    you are using the incorrect "y", they should be quantiles (labeled with probabilities). The following shows the line in the right spot:

    df<-data.frame(x=sort(x),y=qnorm(ppoints(length(x)))) 
    probs <- c(0.01, 0.05, seq(0.1, 0.9, by = 0.1), 0.95, 0.99)
    qprobs<-qnorm(probs)
    
    xl <- quantile(x, c(0.25, 0.75))
    yl <-  qnorm(c(0.25, 0.75))
    slope <- diff(yl)/diff(xl)
    int <- yl[1] - slope * xl[1]
    ggplot(data = df, aes(x = x, y = y)) + geom_point() + geom_abline(intercept = int,slope = slope)+scale_y_continuous(limits=range(qprobs), breaks=qprobs, labels = 100*probs)+labs(y ="Percent" , x="Data")
    

    to add the confidence bounds as in Minitab, you can do the following

    fd<-fitdistr(x, "normal") #Maximum-likelihood Fitting of Univariate Dist from MASS 
    xp_hat<-fd$estimate[1]+qprobs*fd$estimate[2]  #estimated perc. for the fitted normal
    v_xp_hat<- fd$sd[1]^2+qprobs^2*fd$sd[2]^2+2*qprobs*fd$vcov[1,2] #var. of estimated perc
    xpl<-xp_hat + qnorm(0.025)*sqrt(v_xp_hat)  #lower bound
    xpu<-xp_hat + qnorm(0.975)*sqrt(v_xp_hat)  #upper bound
    
    df.bound<-data.frame(xp=xp_hat,xpl=xpl, xpu = xpu,nquant=qprobs)
    

    and add the following two lines to your ggplot from above (in addition, replace the slope and intercept line approach with the estimated percentiles)

    geom_line(data=df.bound,aes(x = xp, y = qprobs))+
    geom_line(data=df.bound,aes(x = xpl, y = qprobs))+
    geom_line(data=df.bound,aes(x = xpu, y = qprobs))
    

提交回复
热议问题