HP Prime for advance statistics - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: HP Prime for advance statistics (/thread-2075.html) HP Prime for advance statistics - pr011235 - 09-04-2014 07:02 AM Does anyone know of a one way anova and or multiple regression app that can be loaded to the HP Prime? RE: HP Prime for advance statistics - Namir - 09-04-2014 03:28 PM (09-04-2014 07:02 AM)pr011235 Wrote:  Does anyone know of a one way anova and or multiple regression app that can be loaded to the HP Prime? I am attaching a pdf file that contains several articles (from the HP Solve newsletter) for the HP 39gII that perform ADVANCED types of regression. These programs should work with the HP Prime. These programs show the power of the HP 39gII and the HP Prime in performing sophisticated regression calculations. Namir PS: Here is the code for about the dozen programs in the attached article, so you can copy and pasted them easily in the HP Prime emulator or the Connectivity Kit. MLR2 Code: ```EXPORT MLR2(MatX,VectY) BEGIN   LOCAL i,j,y,Rsqr;   LOCAL lstDimX,NumRowsX;   LOCAL YMean,Sum1,Sum2,Yhat,RegCoeff;   // calculate the regression coefficients   RegCoeff:=LSQ(MatX,VectY);   // calculate the number of data points   lstDimX:=SIZE(MatX);   NumRowsX:=lstDimX(1);   // calculate ymean   Sum1:=0;   FOR i FROM 1 TO NumRowsX DO     y:=VectY(i,1);     Sum1:=Sum1+y;   END;   YMean:=Sum1/NumRowsX;   // calculate the coefficient of determination   Sum1:=0;   Sum2:=0;   Yhat:=MatX*RegCoeff;   FOR i FROM 1 TO NumRowsX DO     Sum1:=Sum1+(Yhat(i,1)-YMean)^2;     Sum2:=Sum2+(VectY(i,1)-YMean)^2;   END;   Rsqr:=Sum1/Sum2;   // return the results   RETURN {Rsqr,RegCoeff}; END;``` PolyReg Code: ```EXPORT PolyReg(DSMat,SelXCol,SelYCol,Order) BEGIN   LOCAL i,j,x,y;   LOCAL lstDimX,NumRows;   LOCAL MatX, VectY, RegCoeff;   LOCAL YMean,Sum1,Sum2,Yhat,Rsqr;   lstDimX:=SIZE(DSMat);   NumRows:=lstDimX(1);   // initialize matrix and vector   MatX:=MAKEMAT(1,NumRows,Order+1);   VectY:=MAKEMAT(1,NumRows,1);   Sum1:=0;   // populate matrix X and vector y with data   FOR i FROM 1 TO NumRows DO     y:=DSMat(i,SelYCol);     x:=DSMat(i,SelXCol);     VectY(i,1):=y;     Sum1:=Sum1+y;     FOR j FROM 1 TO Order DO       MatX(i,j+1):=x^j;     END;   END;   // calculate ymean   YMean:=Sum1/NumRows;   // calculate regression coefficients   RegCoeff:=LSQ(MatX,VectY);   // calculate the sums of squares of   // (yhat - ymean) and (y - ymean)   Sum1:=0;   Sum2:=0;   FOR i FROM 1 TO NumRows DO     y:=DSMat(i,SelYCol);     x:=DSMat(i,SelXCol);     Yhat:=RegCoeff(1,1);     FOR j FROM 1 TO Order DO       Yhat:=Yhat+RegCoeff(j+1,1)*x^j;     END;     Sum1:=Sum1+(Yhat-YMean)^2;     Sum2:=Sum2+(y-YMean)^2;   END;   // calculate coefficient of determination   Rsqr:=Sum1/Sum2;   RETURN {Rsqr,RegCoeff}; END;``` PowerFit Code: ```EXPORT PowerFit(MatX,VectY) BEGIN   LOCAL i,j;   LOCAL lstDimX,NumRowsX,NumColsX;   LOCAL TMatX,TVectY,RegCoeff,Rsqr;   LOCAL YMean,Sum1,Sum2,Yhat;   // get the number of rows and columns of matrix MatX   lstDimX:=SIZE(MatX);   NumRowsX:=lstDimX(1);   NumColsX:=lstDimX(2);   // create matrices to store transformed data   TMatX:=MAKEMAT(1,NumRowsX,NumColsX+1);   TVectY:=MAKEMAT(1,NumRowsX,1);   FOR i FROM 1 TO NumRowsX DO     TVectY(i,1):=LN(VectY(i,1));     FOR j FROM 1 TO NumColsX DO       TMatX(i,j+1):=LN(MatX(i,j));     END;   END;   // calculate the regression coefficients   RegCoeff:=LSQ(TMatX,TVectY);   // calculate ymean   Sum1:=0;   FOR i FROM 1 TO NumRowsX DO     Sum1:=Sum1+TVectY(i,1);   END;   YMean:=Sum1/NumRowsX;   // calculate the coefficient of determination   Sum1:=0;   Sum2:=0;   Yhat:=TMatX*RegCoeff;   FOR i FROM 1 TO NumRowsX DO     Sum1:=Sum1+(Yhat(i,1)-YMean)^2;     Sum2:=Sum2+(TVectY(i,1)-YMean)^2;   END;   Rsqr:=Sum1/Sum2;   // return the results   RETURN {Rsqr,RegCoeff}; END;``` LinearizedReg Code: ```EXPORT LinearizedReg(DSMat,lstSelXData,lstSelYData) BEGIN   LOCAL i,x,y;   LOCAL lstDimX,NumRowsX;   LOCAL MatX,VectY,RegCoeff;   LOCAL SelXCol,ShiftX,ScaleX,PowerX;   LOCAL SelYCol,ShiftY,ScaleY,PowerY;   LOCAL Sum1,Sum2,YMean,Yhat,Rsqr;      lstDimX:=SIZE(DSMat);   NumRowsX:=lstDimX(1);   // get parameters for x   SelXCol:=lstSelXData(1);   ShiftX:=lstSelXData(2);   ScaleX:=lstSelXData(3);   PowerX:=lstSelXData(4);   // get parameters for y   SelYCol:=lstSelYData(1);   ShiftY:=lstSelYData(2);   ScaleY:=lstSelYData(3);   PowerY:=lstSelYData(4);   // create regression matrix and vector   MatX:=MAKEMAT(1,NumRowsX,2);   VectY:=MAKEMAT(1,NumRowsX,1);   // populate matrix and vector   FOR i FROM 1 TO NumRowsX DO     x:=ScaleX*DSMat(i,SelXCol)+ShiftX;     y:=ScaleY*DSMat(i,SelYCol)+ShiftY;     // transform x     IF PowerX==0 THEN       x:=LN(x);     ELSE       x:=x^PowerX;     END;     // transform y     IF PowerY==0 THEN       y:=LN(y);     ELSE       y:=y^PowerY;     END;     MatX(i,2):=x;     VectY(i,1):=y;   END;   // calculate coefficient of determination   RegCoeff:=LSQ(MatX,VectY);   // calculate ymean   Sum1:=0;   FOR i FROM 1 TO NumRowsX DO     y:=VectY(i,1);     Sum1:=Sum1+y;   END;   YMean:=Sum1/NumRowsX;   // calculate coefficient of determination   Sum1:=0;   Sum2:=0;   Yhat:=MatX*RegCoeff;   FOR i FROM 1 TO NumRowsX DO     Sum1:=Sum1+(Yhat(i,1)-YMean)^2;     Sum2:=Sum2+(VectY(i,1)-YMean)^2;   END;   Rsqr:=Sum1/Sum2;   RETURN {Rsqr,RegCoeff}; END;``` LinearizedReg3 Code: ```EXPORT LinearizedReg3(DSMat,lstSelXData,lstSelZData,lstSelYData) BEGIN   LOCAL i,x,y,z;   LOCAL lstDimX,NumRowsX;   LOCAL MatX,VectY,RegCoeff;   LOCAL SelXCol,ShiftX,ScaleX,PowerX;   LOCAL SelYCol,ShiftY,ScaleY,PowerY;   LOCAL SelZCol,ShiftZ,ScaleZ,PowerZ;   LOCAL Sum1,Sum2,YMean,Yhat,Rsqr;   lstDimX:=SIZE(DSMat);   NumRowsX:=lstDimX(1);   SelXCol:=lstSelXData(1);   ShiftX:=lstSelXData(2);   ScaleX:=lstSelXData(3);   PowerX:=lstSelXData(4);   // get parameters for z   SelZCol:=lstSelZData(1);   ShiftZ:=lstSelZData(2);   ScaleZ:=lstSelZData(3);   PowerZ:=lstSelZData(4);   // get parameters for y   SelYCol:=lstSelYData(1);   ShiftY:=lstSelYData(2);   ScaleY:=lstSelYData(3);   PowerY:=lstSelYData(4);   // create regression matrix and vector   MatX:=MAKEMAT(1,NumRowsX,3);   VectY:=MAKEMAT(1,NumRowsX,1);   // populate matrix and vector   FOR i FROM 1 TO NumRowsX DO     x:=ScaleX*DSMat(i,SelXCol)+ShiftX;     y:=ScaleY*DSMat(i,SelYCol)+ShiftY;     z:=ScaleZ*DSMat(i,SelZCol)+ShiftZ;     // transform x     IF PowerX==0 THEN       x:=LN(x);     ELSE       x:=x^PowerX;     END;     // transform z     IF PowerZ==0 THEN       z:=LN(z);     ELSE       z:=z^PowerZ;     END;     // transform y     IF PowerY==0 THEN       y:=LN(y);     ELSE       y:=y^PowerY;     END;     MatX(i,2):=x;     MatX(i,3):=z;     VectY(i,1):=y;   END;   // calculate regression coefficients   RegCoeff:=LSQ(MatX,VectY);   // calculate ymean   Sum1:=0;   FOR i FROM 1 TO NumRowsX DO     y:=VectY(i,1);     Sum1:=Sum1+y;   END;   YMean:=Sum1/NumRowsX;   // calculate coefficient of determination   Sum1:=0;   Sum2:=0;   Yhat:=MatX*RegCoeff;   FOR i FROM 1 TO NumRowsX DO     Sum1:=Sum1+(Yhat(i,1)-YMean)^2;     Sum2:=Sum2+(VectY(i,1)-YMean)^2;   END;   Rsqr:=Sum1/Sum2;   RETURN {Rsqr,RegCoeff}; END;``` MLRX Code: ```EXPORT MLRX(DSMat,MaxTerms,TrnfMat) BEGIN   LOCAL i,j,x,Rsqr;   LOCAL lstDimX,lstDimT,NumRowsX,NumColsX;   LOCAL MatX,VectY,RegCoeff;   LOCAL SelXCol,Shift,Scale,PowerX;   LOCAL InsMat,InsCol,NumTrnf;   LOCAL YMean,Sum1,Sum2,Yhat;   // calculate the number of data points   lstDimX:=SIZE(DSMat);   NumRowsX:=lstDimX(1);   NumColsX:=lstDimX(2);   // get the number of transformations   lstDimT:=SIZE(TrnfMat);   NumTrnf:=lstDimT(1);   // create the data matrices   MatX:=MAKEMAT(1,NumRowsX,MaxTerms+1);   VectY:=MAKEMAT(1,NumRowsX,1);   FOR j FROM 1 TO NumTrnf DO     // get the transformation/insertion parameters     SelXCol:=TrnfMat[j,1];     Scale:=TrnfMat[j,2];     Shift:=TrnfMat[j,3];     PowerX:=TrnfMat[j,4];     InsMat:=TrnfMat[j,5];     InsCol:=TrnfMat[j,6];     // process all rows for current variable selection,     // transformation, and insertion     FOR i FROM 1 TO NumRowsX DO       // get x       x:=DSMat[i,SelXCol];       // transform x by scaling and shifting       x:=Scale*x+Shift;       // raise x to power or take ln() value       IF PowerX==0 THEN         x:=LN(x);       ELSE         x:=x^PowerX;       END;       // insert in targeted matrix       IF InsMat>0 THEN         // insert in matrix of independent variables         IF InsCol>(MaxTerms+1) THEN           // display an error message           MSGBOX("Column "+InsCol+" is outside the range of columns");           RETURN "ERROR";         END;         MatX[i,InsCol]:=MatX[i,InsCol]*x;       ELSE         // insert in vector of dependent variable         VectY[i,1]:=VectY[i,1]*x;       END;     END;   END;   // calculate the regression coefficients   RegCoeff:=LSQ(MatX,VectY);   // calculate ymean   Sum1:=0;   FOR i FROM 1 TO NumRowsX DO     Sum1:=Sum1+VectY(i,1);   END;   YMean:=Sum1/NumRowsX;   // calculate the correlation coefficient   Sum1:=0;   Sum2:=0;   Yhat:=MatX*RegCoeff;   FOR i FROM 1 TO NumRowsX DO     Sum1:=Sum1+(Yhat(i,1)-YMean)^2;     Sum2:=Sum2+(VectY(i,1)-YMean)^2;   END;   Rsqr:=Sum1/Sum2;   // return the results   RETURN {Rsqr,RegCoeff}; END;``` BestLR Code: ```EXPORT BestLR(Data,lstSel,lstX,lstY) BEGIN   LOCAL Tx,Ty,i,j,k;   LOCAL SelXCol,SelYCol;   LOCAL PowerX,PowerY;   LOCAL MatX,VectY,RegCoeff,MatRes;   LOCAL lstDim,NumRows,ResUpdated;   LOCAL Sum1,Sum2,Rsqr,YMean,Yhat;   LOCAL MaxRes,NumColRes;   // use the default transformation list   // for variable x if lstX is empty   IF SIZE(lstX)==0 THEN     lstX:={-3,-2,-1,-0.5,0,0.5,1,2,3};   END;   // use the default transformation list   // for variable y if lstY is empty   IF SIZE(lstY)==0 THEN     lstY:={-3,-2,-1,-0.5,0,0.5,1,2,3};   END;   // set the maximum number of results   MaxRes:=20;   // set the number of columns in   // the results matrix   NumColRes:=5;   // get the indices for variable x and y   SelXCol:=lstSel(1);   SelYCol:=lstSel(2);   // get the number of rows of data   lstDim:=SIZE(Data);   NumRows:=lstDim(1);   // create the result and regression matrices   MatX:=MAKEMAT(1,NumRows,2);   VectY:=MAKEMAT(1,NumRows,1);   MatRes:=    MAKEMAT(0,MaxRes,NumColRes);   // iterate for each transformation in x   FOR Tx FROM 1 TO SIZE(lstX) DO     // get the current power of x     PowerX:=lstX(Tx);     // transform x     IF PowerX==0 THEN       FOR i FROM 1 TO NumRows DO         MatX(i,2):=LN(Data(i,SelXCol));       END;     ELSE       FOR i FROM 1 TO NumRows DO         MatX(i,2):=Data(i,SelXCol)^PowerX;       END;     END;     // iterate for each transformation in y     FOR Ty FROM 1 TO SIZE(lstY) DO       // get the current power of y       PowerY:=lstY(Ty);       // transform y       IF PowerY==0 THEN         FOR i FROM 1 TO NumRows DO           VectY(i,1):=LN(Data(i,SelYCol));         END;       ELSE         FOR i FROM 1 TO NumRows DO           VectY(i,1):=Data(i,SelYCol)^PowerY;         END;       END;       // calculate regression coefficients       RegCoeff:=LSQ(MatX,VectY);       // calculate ymean       Sum1:= 0;       FOR i FROM 1 TO NumRows DO         Sum1:=Sum1+VectY(i,1);       END;       YMean:=Sum1/NumRows;       // calculate coefficient of determination       Sum1:=0;       Sum2:=0;       Yhat:=MatX*RegCoeff;       FOR i FROM 1 TO NumRows DO         Sum1:=Sum1+(Yhat(i,1)-YMean)^2;         Sum2:=Sum2+(VectY(i,1)-YMean)^2;       END;       Rsqr:=Sum1/Sum2;       // Rsqr is better than last entry       // in the results matrix?       IF Rsqr>MatRes(MaxRes,1) THEN         ResUpdated:=0;         // loop for each row in the         // results matrix         FOR i FROM 1 TO MaxRes DO           // compare with other Rsqr values           IF ResUpdated==0 AND Rsqr>MatRes(i,1) THEN             // found better Rsqr for row i             // push rows below?             IF iMatRes(MaxRes,1) THEN           ResUpdated:=0;           // check which row to insert better           // regression results           FOR i FROM 1 TO MaxRes DO             // insert new results in row i?             IF ResUpdated==0 AND Rsqr>MatRes(i,1) THEN               // inserting inside the results matrix?               IF i