1  REM  ****  HP BASIC PROGRAM LIBRARY  *******************************
2  REM
3  REM        REGRES:   STEP-WISE REGRESSION
4  REM
5  REM        36738 REV  A   10/73
6  REM
7  REM  ****  CONTRIBUTED PROGRAM  ************************************
10  REM  REGRES--REALIZA ANALISIS DE REGRESIONES A PASOS
20  DIM Z(10,10),A(10),L(10),B(10),P(10),X(50,10)
30  PRINT "REGRESION A PASOS"
40  PRINT
50  PRINT "RESPONDA LAS PREGUNTAS CON N PARA NO, Y PARA SI, O E PARA EXPLICAR."
60  RESTORE
70  READ N,M
80  LET P4=N*M
90  IF P4>1000 THEN 2420
100  IF P4 <= 0 THEN 2420
110  IF N>100 THEN 2420
120  IF M>10 THEN 2420
130  FOR J=1 TO N
140  FOR K=1 TO M
150  READ X(J,K)
160  NEXT K
170  NEXT J
180  FOR J=1 TO M
190  LET L(J)=1
200  LET A(J)=0
210  LET B(J)=0
220  LET P(J)=0
230  NEXT J
240  FOR K=1 TO M
250  FOR J=1 TO N
260  LET A(K)=A(K)+X(J,K)
270  NEXT J
280  LET A(K)=A(K)/N
290  NEXT K
300  LET P1=0
310  LET P2=0
320  FOR J=1 TO M
330  FOR K=1 TO M
340  LET S1=0
350  LET S2=0
360  LET S3=0
370  FOR J1=1 TO N
380  LET P1=X(J1,J)-X(1,J)
390  LET P2=X(J1,K)-X(1,K)
400  LET S1=S1+P1*P2
410  LET S2=S2+P1
420  LET S3=S3+P2
430  NEXT J1
440  LET Z(J,K)=S1-S2*S3/N
450  IF J <> K THEN 470
460  IF Z(J,K) <= 0 THEN 2450
470  NEXT K
480  NEXT J
490  LET N4=0
500  PRINT
510  PRINT "INGRESE EL NUMERO DE COLUMNAS ( 1 -"; M; ") CORRESPONDIENTE A LA "
520  PRINT "VARIABLE DEPENDIENTE(Y).";
530  INPUT N1
540  IF N1>M THEN 580
550  IF N1<1 THEN 580
560  IF INT(N1)<N1 THEN 580
570  GOTO 600
580  PRINT "SU DATO NO CONTIENE LA VARIABLE ";N1;". TIPEE NUEVAMENTE POR FAVOR."
590  GOTO 530
600  PRINT
610  PRINT "DESEA OMITIR UNA VARIABLE(XK) DEL";
620  PRINT " ANALISIS";
630  INPUT D$
640  IF D$="N" OR D$="n" THEN 1020
650  IF D$="Y" OR D$="y" THEN 710
660  PRINT
670  PRINT "SI NO DESEA INGRESAR CIERTAS VARIABLES"
680  PRINT "EN LA REGRESION, PUEDE DETENERLAS DE INGRESARLAS"
690  PRINT "EN EL PRESENTE ANALISIS."
700  GOTO 600
710  PRINT "CUANDO SE PIDA, INGRESE LA COLUMNA CORRESPONDIENTE A"
720  PRINT "LA VARIABLE A OMITIR. TIPEE 0 PARA TERMINAR LA SOLICITUD."
730  PRINT
740  LET P1=0
750  PRINT "VARIABLE, POR FAVOR";
760  INPUT D1
770  IF D1=0 THEN 910
780  IF D1=N1 THEN 890
790  IF D1>M THEN 870
800  IF D1<0 THEN 870
810  IF INT(D1)<D1 THEN 870
820  IF L(D1)<0 THEN 750
830  LET P1=P1+1
840  LET N4=N4+1
850  LET L(D1)=-1
860  GOTO 750
870  PRINT "IMPOSIBLE--LA VARIABLE NO EXISTE. INTENTE DE NUEVO."
880  GOTO 750
890  PRINT "OH! ESTA INTENTANDO OMITIR Y. INTENTE DE NUEVO."
900  GOTO 750
910  IF P1=0 THEN 970
920  IF P1 >= M-1 THEN 940
930  GOTO 1020
940  FOR J=1 TO M
950  LET L(J)=1
960  NEXT J
970  PRINT
980  PRINT "HA PREGUNTADO OMITIR TODAS O NINGUNA DE ";
990  PRINT "LAS VARIABLES.  REPITO-"
1000  LET N4=0
1010  GOTO 610
1020  LET M1=M-N4
1030  LET Z1=Z(N1,N1)
1040  PRINT
1050  PRINT
1060  LET V1=1
1070  LET Q2=0
1080  LET R3=0
1090  LET N3=M1-1
1100  FOR S1=1 TO N3
1110  PRINT
1120  PRINT
1130  PRINT "STEP "S1
1140  LET Q1=0
1150  FOR J=1 TO M
1160  IF J=N1 THEN 1220
1170  IF L(J) <= 0 THEN 1220
1180  LET R5=Z(J,N1)^2/Z(J,J)
1190  IF Q1 >= R5 THEN 1220
1200  LET Q1=R5
1210  LET N5=J
1220  NEXT J
1230  PRINT
1240  PRINT "LA VARIABLE SELECCIONADA ES ... X"N5
1250  LET L(N5)=0
1260  PRINT "SUMA DE RAICES REDUCIDAS EN ESTE PASO....";Q1
1270  LET R5=Q1/Z1
1280  PRINT "PROPORCION DE VARIACION DE Y REDUCIDA....";R5
1290  LET Q2=Q2+Q1
1300  LET R3=R3+R5
1310  LET J=N-S1-1
1320  LET R4=(Z1-Q2)/J
1330  LET T1=Q1/R4
1340  IF T1 <= 10000 THEN 1360
1350  LET T1=10000
1360  PRINT "PARCIAL F (D.F. =)";J;"...............";T1
1370  IF S1=1 THEN 1510
1380  LET V1=S1-1
1390  PRINT
1400  PRINT "DESEA INGRESAR ESTA VARIABLE NE LA REGRESION";
1410  INPUT D$
1420  IF D$="N" OR D$="n" THEN 2200
1430  IF D$="Y" OR D$="y" THEN 1510
1440  PRINT
1450  PRINT "EN UN PASO DADO; LA VARIABLE QUE REDUCE LA MAYOR"
1460  PRINT "CANTIDAD DE SIMA DE RAICES ES ELEGIDA. SI LA REDUCCION "
1470  PRINT "INDICADA POR ENVIMA DE LA LINEA 3 ES SIGNIFICANTE, INGRESE ESTA"
1480  PRINT "VARIABLE EN LA REGRESION; CASO CONTRARIO, LA SELECCION DE "
1490  PRINT "VARIABLES FINALIZARA."
1500  GOTO 1390
1510  LET M1=S1
1520  LET V1=S1
1530  LET P(S1)=N5
1540  PRINT
1550  PRINT "SUMA ACUMULATIVA DE SUMA DE RAICES REDUCIDA......";Q2
1560  PRINT "PROPORCION ACUMULATIVA REDUCIDA..........";R3;"(OF"Z1")"
1570  LET R5=SQR(R3)
1580  PRINT
1590  PRINT "COHEFICIENTE DE CORRELACION MULTIPLE.......";R5
1600  LET R5=S1
1610  LET R5=(Q2/R5)/R4
1620  IF R5 <= 10000 THEN 1640
1630  LET R5=10000
1640  PRINT "F PARA ANALISIS DE VAR. (D.F. ="S1;",";J;")";R5
1650  LET S5=SQR(R4)
1660  PRINT "ESTIMADO DE ERROR ESTANDAR ESTIMADO.............";S5
1670  LET R4=Z(N5,N5)
1680  FOR J=1 TO M
1690  IF L(J)>0 THEN 1730
1700  IF L(J)<0 THEN 1760
1710  IF J=N5 THEN 1750
1720  LET Z(J,J)=Z(J,J)+Z(N5,J)*Z(N5,J)/R4
1730  LET Z(N5,J)=Z(N5,J)/R4
1740  GOTO 1760
1750  LET Z(N5,N5)=1/R4
1760  NEXT J
1770  LET B(S1)=Z(N5,N1)
1780  IF S1=1 THEN 1900
1790  LET J2=S1-1
1800  FOR J=1 TO J2
1810  LET J3=S1-J
1820  LET J4=P(J3)
1830  LET B(J3)=Z(J4,N1)
1840  FOR K=1 TO J
1850  LET K1=S1-K+1
1860  LET K2=P(K1)
1870  LET B(J3)=B(J3)-Z(J4,K2)*B(K1)
1880  NEXT K
1890  NEXT J
1900  PRINT
1910  PRINT "VARIABLE","COHEF. REG.","COHEF-ERR. STD.",TAB(50);"COMPUTADO T"
1920  LET B1=A(N1)
1930  FOR J=1 TO S1
1940  LET K3=P(J)
1950  LET Q1=S5*SQR(Z(K3,K3))
1960  LET T1=B(J)/Q1
1970  LET B1=B1-B(J)*A(K3)
1980  PRINT K3,B(J),Q1,TAB(50);T1
1990  NEXT J
2000  PRINT
2010  PRINT "INTERCEPT(A) "B1
2020  FOR J=1 TO M
2030  IF L(J) <= 0 THEN 2100
2040  FOR K=1 TO M
2050  IF L(K)<0 THEN 2080
2060  IF K=N5 THEN 2080
2070  LET Z(J,K)=Z(J,K)-Z(J,N5)*Z(N5,K)
2080  NEXT K
2090  LET Z(J,N5)=Z(J,N5)/(-R4)
2100  NEXT J
2110  NEXT S1
2120  GOTO 2200
2130  PRINT
2140  PRINT "DESEA COMPUTAR MAS REGRESIONES";
2150  INPUT D$
2160  IF D$="N" OR D$="n" THEN 2490
2170  IF D$="Y" OR D$="y" THEN 60
2180  GOTO 2130
2190  GOTO 60
2200  PRINT
2210  PRINT "DESEA IMPRIMIR LA TABLA DE RESIDUALES";
2220  INPUT D$
2230  IF D$="N" OR D$="n" THEN 2130
2240  IF D$="Y" OR D$="y" THEN 2290
2250  PRINT
2260  PRINT "SI DESEA IMPRIMIR Y OBSERVADA, Y ESTIMADA, RESIDUAL, Y"
2270  PRINT "VALOR ESTANDARIZADO DE RESIDUAL PARA CADA CASO, PRESIONE Y PARA YES."
2280  GOTO 2200
2290  PRINT
2300  PRINT "OBS. NO.","Y OBSERVADA","Y ESTIMADA","RESIDUAL","RESID. STD."
2310  FOR J=1 TO N
2320  LET E1=B1
2330  FOR K=1 TO V1
2340  LET K1=P(K)
2350  LET E1=E1+B(K)*X(J,K1)
2360  NEXT K
2370  LET R6=X(J,N1)-E1
2380  LET S6=R6/S5
2390  PRINT J,X(J,N1),E1,R6,S6
2400  NEXT J
2410  GOTO 2130
2420  PRINT
2430  PRINT "SUS VALORES DE M y/O N SON INACEPTABLES. REVISELAS."
2440  STOP
2450  PRINT "UN VALOR DE CERO PARA UN ELEMENTO DE LA DIAGONAL DE LA"
2460  PRINT "MATRIZ DE PRODUCTO CRUZADO HA SIDO COMPUTADA. REVISE DATOS."
2470  PRINT "ESTO ES CAUSADO "'2"POR UNA VARIABLE CON UN VALOR CONSTANTE"
2480  PRINT "PARA TODAS OBSERVACIONES."
2490  STOP
5000  DATA 15,4
5001  DATA 32,48,54,15
5002  DATA 36,33,19,16
5003  DATA 3,28,30,14
5004  DATA 12,33,64,22
5005  DATA 36,34,60,24
5006  DATA 24,36,53,19
5007  DATA 19,42,29,13
5008  DATA 20,33,55,15
5009  DATA 27,36,62,23
5010  DATA 15,22,33,12
5011  DATA 45,46,68,25
5012  DATA 9,28,42,17
5013  DATA 11,32,45,18
5014  DATA 33,34,39,19
5015  DATA 21,45,39,18
9999  END