Calcite | Level 5

## calculate the location parameter with respect to Chi-Square distribution using RANNOR

I am a new user of SAS with no basic knowledge. I want to calculate the location parameter with respect to Chi-Square distribution using RANNOR in SAS/IML. However, I don't know how to write the programming. Can you help me?? It is very urgent as I am in my final semester of my postgraduate study. Appreciate your help so much.

10 REPLIES 10
Super User

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

You can also review his blog here :

https://blogs.sas.com/content/author/rickwicklin/

Calcite | Level 5

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

I am using Hodges-Lehmann estimator and want to test under Chi-Square (3df). The attached file is the formula of estimator and the distribution. Below is my programming and there are errors when I run it. May I know which parts should I change?

PROC IML;

**MATRIX CALLED GSMAT**;

START GBASMOD(Y,GSMAT)  GLOBAL (NX,BOBS);

GSMAT = J(2, BOBS, 0);

F = 1;

M = 0;

DO I = 1 TO BOBS;

SAMP = NX[I];

L = M + SAMP;

TEMP = Y[F:L];

**GETTING GROUP MEDIANS**;

/*GSMAT[1,I] = HL – location estimator*/

H=(SAMP)*(SAMP+1)/2;

AVG=J(H,1,0);

K=0;

DO Z=1 TO SAMP;

DO J=1 TO SAMP;

IF Z<=J THEN DO;

K=K+1;

AVG[K,]=(TEMP[Z]+TEMP[J])/2;

END;

END;

GSMAT[1,I] = MEDIAN(AVG);

END;

****GENERATE DATA FOR CONDITIONS****;

*PRINT 'NSIM STAT PVAL ';

DO K = 1 TO 1000000;

IF F = 1 THEN TEMP=RANNOR(J(1000000,3,439839383));

NX[I] = TEMP[##,];

END;

RUN

PRINT 'NSIM STAT PVAL’;

NAMES1 = {"Estimate"};

Calcite | Level 5

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

I tried with another programming but there are errors. Can anyone help me to identify my mistakes?

PROC IML;

**HODGES-LEHMANN IN CHI-SQUARE DISTRIBUTION**;

SSEED=439839383;

N=1000000;

TEMP=RANNOR(J(N,3,SSEED));

YTEMP=TEMP[##2,+];

Y1=YTEMP[I];

Y2=YTEMP[J];

DO I=1 TO N;

DO J=1 TO N;

AVG=(Y1+Y2)/2;

HL=MEDIAN(AVG);

END;

RUN

PRINT 'RESULTS:';

NAMEHL = {"HL"};

END;

SAS Super FREQ

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

You seem to be asking two questions. The first is how to compute the Hodges-Lehmann estimator for a set of data. The second issue is that you seem to be trying to generate random variates from the chi-square distribution with 3 degrees of freedom by using random normal variates.

You don't need to generate normal variates to generate random values from the chi-square distribution. You can generate chi-square values directly from the RANDGEN function. I won't pursue this because you don't need random variates to compute the p-value for the H-L test. P-values are computed by using the CDF function to compute probabilities. Read the article "Four essential functions for statistical programmers" to understand the difference between PDF, CDF, quantiles, and random values.

As for computing the H-L estimator, you don't need PROC IML for this. You can use the HL option in the PROC NPAR1WAY procedure. The documentation has an example that shows how to generate the estimate and how to interpret the result. Follow that example to analyze your data.

Calcite | Level 5

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

Thank you so much for your reply. However, i have to use PROC IML due to the requirement. I manage to create another programming but i cant get the results from SAS. May i know which part is wrong?

PROC IML;
START MYFUNC(HL);
HL=MEDIAN(AVG);
FINISH;

SSEED=439839383;
N=1000000;
TEMP=RANNOR(J(N,3,SSEED));
YTEMP=TEMP[,##];

/*HODGES-LEHMANN estimator*/
K=0;
DO Z=1 TO YTEMP;
DO J=1 TO YTEMP;
IF Z<=J THEN DO;
K=K+1;
AVG[K,]=(YTEMP[Z]+YTEMP[J])/2;
END;
END;

CALL MYFUNC(HL);
PRINT HL;

SAS Super FREQ

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

>  i have to use PROC IML due to the requirement.

I don't understand. What requirement? Is this for a school assignment?

Calcite | Level 5

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

Dear Sir,

Yes. It is the requirement of my assignment. Description of my assignment is "the value of θ are determined by calculating θ with one million observations that were simulated from Chi-Square (3df)".

Below are the steps for me to create the programming using PROC IML with N=1000000.

1. Generate 3 standard random variates (use RANNOR).

2. Square each variate.

3. Sum all three squared variates.

4. Use Hodges-Lehmann to calculate the value of θ.

Barite | Level 11

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

Here are a few hints on how to get the first program above to run.

1. Check the number of DO and END statements as these need to balance.
2. "TO YTEMP" in the DO loops is wrong.  IML is expecting a scalar, but you are providing a matrix.
3. You must create the matrix AVG before you try to assign its elements inside the loop.
Calcite | Level 5

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

I am sorry to disturb everyone. I am a new user and I have zero knowledge on this software.

can i do this? i still can't get any results from SAS.

PROC IML;

START MYFUNC(HL);

HL=MEDIAN(AVG);

FINISH;

SSEED=439839383;

N=1000000;

TEMP=RANNOR(J(N,3,SSEED));

YTEMP=TEMP[,##];

/*HODGES-LEHMANN estimator*/

H=N*(N+1)/2;

AVG=J(H,1,0);

K=0;

DO Z=1 TO N;

DO J=1 TO N;

IF Z<=J THEN DO;

K=K+1;

AVG[K,]=(YTEMP[Z]+YTEMP[J])/2;

END;

END;

START HL;

CALL MYFUNC(HL);

RUN;

PRINT HL;

SAS Super FREQ

## Re: calculate the location parameter with respect to Chi-Square distribution using RANNOR

Yes, it can be done. It sounds ilke you are supposed to compute the test statistic and then estimate the p-value by using a Monte Carlo simulation from the chi-square distribution with 3 DoF. Here are some hints:

1. Read "How to compute p-values" to learn how to estimate a p-value when you have a test statistic and N values from the  simulated chi-square distribution
2. The blog at https://blogs.sas.com/content/iml has many simulation articles. If you go to your favorite search engine and type keywords site:https://blogs.sas.com/content/iml   then your search is restricted to only that site.

Here's what you should do:

A. Figure out how to calculate the H-L statistic for your data. THis is the "test statistic."

B. You've already generated N random observations from chi-square(3) distribution.

C. Use the formula in the "How to compute p-values..." article to estimate the p-value for your test statistic.

Good luck.

From The DO Loop