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.
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.
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/
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."
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.
Thank you for your help. It was very useful and my code works now.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.