RegArchOptimParams::RegArchOptimParams(SEXP theSEXP, uint theNRegArchParam, const char* theMethod, double theLower, double theUpper) { mvMethod = new char[strlen(theMethod)+1]; strcpy(mvMethod, theMethod); mvLower = theLower; mvUpper = theUpper; cRUtil myRUtil; uint myParamNumber = length(theSEXP); char** myNames = new char*[myParamNumber]; for (register uint i = 0; i < myParamNumber; i++) myNames[i] = new char[16]; // Should be enough to store any parameter name cDVector* myVal = new cDVector[myParamNumber]; uint* mySizes = new uint[myParamNumber]; myRUtil.GetListVectSizesSexp(theSEXP, 0, myParamNumber, mySizes); for (register uint i = 0; i < myParamNumber; i++) myVal[i] = cDVector(mySizes[i]); myRUtil.GetListNamedVectSexp(theSEXP, 0, myParamNumber, myNames, myVal); FetchParameters(myParamNumber, myNames, myVal, theNRegArchParam); for (register uint i = 0; i < myParamNumber; i++) delete[] myNames[i]; delete[] myNames; delete[] myVal; delete[] mySizes; }
SEXP RRegArchFit(SEXP theInitPar, SEXP theNObs, SEXP theYt, SEXP theModel, SEXP theMethod, SEXP theLower, SEXP theUpper, SEXP theControl) {SEXP myResSEXP; #ifdef _GSL_ cRUtil myRUtil; int myNObs = INTEGER(theNObs)[0]; cDVector myYt(myNObs); myRUtil.GetVectSexp(theYt, 0, myYt); cDMatrix myMatX; cRegArchParam myParam; SexpToRegArchParam(theModel, myParam, &myMatX); cDVector myInitPar(myParam.GetNParam()); myRUtil.GetVectSexp(theInitPar, 0, myInitPar); cDMatrix *myMat; if ( (myMatX.GetNRow() > 0) && (myMatX.GetNCol() > 0) ) myMat = &myMatX; else myMat = NULL; cRegArchValue myValue = cRegArchValue(&myYt, myMat); const char* myMethod = CHAR(STRING_ELT(theMethod,0)); double myLower = REAL(theLower)[0]; double myUpper = REAL(theUpper)[0]; RegArchOptimParams myOptimParams(theControl, myParam.GetNParam(), myMethod, myLower, myUpper); sRegArchOptimResult myRes = RegArchOptimize(myInitPar, myParam, myValue, myOptimParams); char *myNames[3] = {"par", "value", "convergence"}; cDVector myResVect[3] = {myRes.mPar, cDVector(1, myRes.mValue), cDVector(1, myRes.mConvergence)}; myRUtil.SetListNamedVectSexp(myResVect, myNames, 3, myResSEXP); myRUtil.EndProtect(); #endif return(myResSEXP); }
void LapackInvAndDet(cDMatrix& theMatrix, cDMatrix& theInvMatrix, double& theDet) { uint myNCol = theMatrix.GetNCols() ; double *myAP = new double[myNCol*(myNCol + 1)/2], *myW = new double[myNCol], *myZ = new double[myNCol*myNCol], *myWork = new double[myNCol * 3] ; int myInfo, myN = (int)(myNCol), myldz = (int)(myNCol) ; for (register int i = 0 ; i < myN ; i++) for (register int j = i ; j < myldz ; j++) myAP[i+(j+1)*j/2] = theMatrix[i][j] ; F77_NAME(dspev)("V", "U", &myN, myAP, myW, myZ, &myldz, myWork, &myInfo) ; if (myInfo != 0) throw cOTError("Non inversible matrix") ; theDet = 1.0L ; cDVector myInvEigenValue = cDVector(myNCol) ; cDMatrix myEigenVector(myNCol, myNCol) ; for (register uint i = 0 ; i < myNCol ; i++) { theDet *= myW[i] ; myInvEigenValue[i] = 1.0 /myW[i] ; for (register int j = 0 ; j < myN ; j++) myEigenVector[i][j] = myZ[i + j*myN] ; } theInvMatrix = myEigenVector ; cDMatrix myAuxMat1 = Diag(myInvEigenValue), myAuxMat2 = Transpose(myEigenVector) ; cDMatrix myAuxMat = myAuxMat1 * myAuxMat2 ; theInvMatrix = theInvMatrix * myAuxMat ; delete myAP ; delete myW ; delete myZ ; delete myWork ; }