BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pariz
Fluorite | Level 6

Hello,

I'm fitting a mixture of one truncated normal distribution and 1 normal distribution to my data. The code is:

proc fmm data=observations plot=density(bins=56);

model x = / dist=normal kmax=1 parms(8 1);

model +/ dist=truncnormal(3,.);

I want to export the resulting pdf for both distributions and added together. I've read through FMM manual, but I couldn't find the easy way of doing it, so I used another approach:

data pdf;

  set example;

do x = 0 to 13 by 0.01;

      f1 = Prior_1 * (1/ ((Var_1)**(1/2) * 2.506628)) * exp(-((x - Pred_1)**2) / (2 * (Var_1)**(1/2) * (Var_1)**(1/2)));

     f2 = Prior_2 * (1/(cunormal(3-Pred_2)/(Var_2)**(1/2))(1/ ((Var_2)**(1/2) * 2.506628)) * exp(-((x - Pred_2)**2) / (2 * (Var_2)**(1/2) * (Var_2)**(1/2)));

      f = f1 + f2;

      output pdf;

keep f1 f2 f x;

  end;

run;

It works fine with normal distribution, but how can I get the term  (1/(cunormal(1.38-Pred_2)/(Var_2)**(1/2)) to work? Since there is no such function is SAS. Could someone please advise me how I can get the actual pdf for truncated normal and export it?

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

Yes, you can implement the function in the DATA step. The CDF and PDF functions are both available in the DATA step. In place of the CHOOSE function, you can use an IF-THEN/ELSE statement.

You can inline the computation, so no need to define the pdfTN function.

View solution in original post

4 REPLIES 4
Rick_SAS
SAS Super FREQ

You should use the PDF function to produce the PDF for the normal distribution. For details on the PDF function (and the related CDF, QUANTILE, and RAND functions) see Four essential functions for statistical programmers - The DO Loop

You can also use the PDF of the normal distribution to compute the PDF for the truncated normal distribution. See http://blogs.sas.com/content/iml/2013/07/24/the-truncated-normal-in-sas/

Pariz
Fluorite | Level 6

Thank you for your reply. Is there a way to write the following code without using proc iml?

start pdfTN(x, mu, sigma, a, b);

  /* Support one-sided truncation. Define Phi(.M)=0 and Phi(.P)=1 */

  cdf_a = choose(a=., 0, cdf("Normal", a, mu, sigma)); /* Phi(a) */

  cdf_b = choose(b=., 1, cdf("Normal", b, mu, sigma)); /* Phi(b) */

  return( pdf("Normal", x, mu, sigma) / (cdf_b - cdf_a) );

finish;

Or how can I access the function pdfTN in my code?

data pdf;

  set example;

do x = 0 to 13 by 0.01;

  f1=Prior_1*pdf("Normal",x,Pred_1,Var_1);

  f2=Prior_2*pdfTN(x,Pred_2,Var_2,3,8);

output pdf;

keep f1 f2;

end;

run;

When I run the code, the error message is  "The function PDFTN is unknown, or cannot be accessed."

Rick_SAS
SAS Super FREQ

Yes, you can implement the function in the DATA step. The CDF and PDF functions are both available in the DATA step. In place of the CHOOSE function, you can use an IF-THEN/ELSE statement.

You can inline the computation, so no need to define the pdfTN function.

Pariz
Fluorite | Level 6

Thank you for your help. It was very useful and my code works now.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 1689 views
  • 3 likes
  • 2 in conversation