inst/unittests/runit.importMethodBlock.R

# $LastChangedDate: 2010-01-12 15:55:59 +0000 (Tue, 12 Jan 2010) $
# $LastChangedBy: fgochez $
# $Rev: 14609 $
# 
# Author: fgochez
###############################################################################


# tests the low-level function .importMethodBlock (in importNmLstNM7)

test.importMethodBlock <- function()
{
	
	# sample method block
	testBlock <- c(" #METH: Iterative Two Stage", " EM/BAYES SETUP", " THETAS THAT ARE MU MODELED:", 
			"   1   2   3   4", " THETAS THAT ARE SIGMA-LIKE:", " ", " ", 
			" MONITORING OF SEARCH:", "", " iteration            0 OBJ=   -439.445868533902     ", 
			" iteration            5 OBJ=   -1119.48137768895     ", " iteration           10 OBJ=   -1120.19191233229     ", 
			" iteration           15 OBJ=   -1120.26385480453     ", " iteration           20 OBJ=   -1120.28483467697     ", 
			" iteration           25 OBJ=   -1120.29421562205     ", " iteration           30 OBJ=   -1120.29934078386     ", 
			" iteration           35 OBJ=   -1120.30236183088     ", " iteration           40 OBJ=   -1120.30417175997     ", 
			" iteration           45 OBJ=   -1120.30528535008     ", " iteration           50 OBJ=   -1120.30595982428     ", 
			" iteration           55 OBJ=   -1120.30637009744     ", " iteration           60 OBJ=   -1120.30662747964     ", 
			" iteration           65 OBJ=   -1120.30677846680     ", " iteration           70 OBJ=   -1120.30687589562     ", 
			" iteration           75 OBJ=   -1120.30693225876     ", " iteration           80 OBJ=   -1120.30696556872     ", 
			" iteration           85 OBJ=   -1120.30699553335     ", " iteration           90 OBJ=   -1120.30701400145     ", 
			" iteration           95 OBJ=   -1120.30702142508     ", " iteration          100 OBJ=   -1120.30702424494     ", 
			" iteration          105 OBJ=   -1120.30702412937     ", " iteration          110 OBJ=   -1120.30702950237     ", 
			" Convergence achieved", " Elapsed estimation time in seconds:     9.99", 
			" ", " #TERM:", " OPTIMIZATION COMPLETED", " ", "", " ETABAR IS THE ARITHMETIC MEAN OF THE ETA-ESTIMATES,", 
			" AND THE P-VALUE IS GIVEN FOR THE NULL HYPOTHESIS THAT THE TRUE MEAN IS 0.", 
			"", " ETABAR:         4.1783E-08  2.5281E-08  2.2517E-08  1.3946E-08", 
			" SE:             3.9054E-02  2.9143E-02  3.5180E-02  3.4116E-02", 
			"", " P VAL.:         1.0000E+00  1.0000E+00  1.0000E+00  1.0000E+00", 
			" ", " ETAshrink(%):   3.3135E+00  1.9596E+01  2.2806E+01  1.4537E+01", 
			" EPSshrink(%):   3.1694E+01", " ", " #TERE:", " Elapsed covariance time in seconds:     0.05", 
			"1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", 
			" ", " ", " ", " ", " ", " ", " ", " ", " ************************************************************************************************************************", 
			" ********************                                                                                ********************", 
			" ********************                               ITERATIVE TWO STAGE                              ********************", 
			" #OBJT:**************                        FINAL VALUE OF OBJECTIVE FUNCTION                       ********************", 
			" ********************                                                                                ********************", 
			" ************************************************************************************************************************", 
			" ", "", "", "", "", "", " #OBJV:********************************************    -1120.307       **************************************************", 
			"1", " ************************************************************************************************************************", 
			" ********************                                                                                ********************", 
			" ********************                               ITERATIVE TWO STAGE                              ********************", 
			" ********************                             FINAL PARAMETER ESTIMATE                           ********************", 
			" ********************                                                                                ********************", 
			" ************************************************************************************************************************", 
			" ", "", "", " THETA - VECTOR OF FIXED EFFECTS PARAMETERS   *********", 
			"", "", "         TH 1      TH 2      TH 3      TH 4     ", " ", 
			"         1.68E+00  1.59E+00  8.13E-01  2.37E+00", " ", "", "", 
			" OMEGA - COV MATRIX FOR RANDOM EFFECTS - ETAS  ********", "", 
			"", "            ETA1      ETA2      ETA3      ETA4", " ", " ETA1", 
			"+        1.65E-01", " ", " ETA2", "+        4.13E-03  1.33E-01", 
			" ", " ETA3", "+        5.38E-03  1.72E-02  2.10E-01", " ", " ETA4", 
			"+       -1.61E-02  1.23E-02  5.08E-02  1.61E-01", " ", "", "", 
			" SIGMA - COV MATRIX FOR RANDOM EFFECTS - EPSILONS  ****", "", 
			"", "            EPS1", " ", " EPS1", "+        5.53E-02", " ", 
			"1", " ************************************************************************************************************************", 
			" ********************                                                                                ********************", 
			" ********************                               ITERATIVE TWO STAGE                              ********************", 
			" ********************                          STANDARD ERROR OF ESTIMATE (S)                        ********************", 
			" ********************                                                                                ********************", 
			" ************************************************************************************************************************", 
			" ", "", "", " THETA - VECTOR OF FIXED EFFECTS PARAMETERS   *********", 
			"", "", "         TH 1      TH 2      TH 3      TH 4     ", " ", 
			"         4.61E-02  4.90E-02  6.52E-02  5.39E-02", " ", "", "", 
			" OMEGA - COV MATRIX FOR RANDOM EFFECTS - ETAS  ********", "", 
			"", "            ETA1      ETA2      ETA3      ETA4", " ", " ETA1", 
			"+        2.89E-02", " ", " ETA2", "+        2.31E-02  3.48E-02", 
			" ", " ETA3", "+        3.24E-02  3.82E-02  6.49E-02", " ", " ETA4", 
			"+        2.76E-02  2.73E-02  4.51E-02  4.05E-02", " ", "", "", 
			" SIGMA - COV MATRIX FOR RANDOM EFFECTS - EPSILONS  ****", "", 
			"", "            EPS1", " ", " EPS1", "+        7.72E-03", " ", 
			"1", " ************************************************************************************************************************", 
			" ********************                                                                                ********************", 
			" ********************                               ITERATIVE TWO STAGE                              ********************", 
			" ********************                        COVARIANCE MATRIX OF ESTIMATE (S)                       ********************", 
			" ********************                                                                                ********************", 
			" ************************************************************************************************************************", 
			" ", "", "            TH 1      TH 2      TH 3      TH 4      OM11      OM12      OM13      OM14      OM22      OM23      OM24      OM33", 
			"             OM34      OM44      SG11", " ", " TH 1", "+        2.13E-03", 
			" ", " TH 2", "+        3.66E-04  2.40E-03", " ", " TH 3", "+        3.64E-04  2.18E-04  4.25E-03", 
			" ", " TH 4", "+       -2.55E-05  1.60E-04  1.61E-03  2.91E-03", 
			" ", " OM11", "+       -2.46E-04  9.76E-05  7.27E-05 -8.56E-05  8.37E-04", 
			" ", " OM12", "+        1.38E-05  1.77E-04  3.65E-05 -1.34E-04  3.11E-04  5.33E-04", 
			" ", " OM13", "+       -1.66E-04 -1.41E-06 -2.52E-04  2.27E-04  1.41E-04  2.40E-04  1.05E-03", 
			" ", " OM14", "+       -1.79E-04 -2.14E-04  1.49E-04  1.76E-04  2.22E-04  1.82E-04  5.45E-04  7.64E-04", 
			" ", " OM22", "+       -3.84E-05 -1.99E-05 -2.09E-04 -1.13E-04  1.40E-04  2.37E-04  8.43E-05  1.39E-04  1.21E-03", 
			" ", " OM23", "+       -7.76E-05 -8.97E-05  6.47E-05  1.26E-04  1.88E-04  3.92E-05 -6.21E-05 -3.03E-05  2.81E-04  1.46E-03", 
			" ", " OM24", "+       -2.59E-04 -1.03E-04  1.42E-04  1.55E-04  1.15E-04  8.62E-05  2.30E-05  9.12E-05  3.79E-04  5.84E-04  7.47E-04", 
			" ", " OM33", "+       -3.25E-04 -3.28E-04 -1.99E-04  5.31E-04  2.23E-04  1.48E-04  8.08E-04  3.88E-04  5.47E-04  4.35E-04  4.05E-04  4.21E-03", 
			" ", " OM34", "+       -2.06E-05 -9.04E-05  2.63E-04  2.74E-04  2.52E-04  1.17E-04  2.93E-04  2.20E-04  3.80E-04  3.73E-04  3.34E-04  2.20E-03", 
			"          2.04E-03", " ", " OM44", "+        3.13E-05 -1.72E-05  1.96E-04 -2.92E-04  2.72E-04  1.27E-04  6.60E-05  1.56E-04  3.27E-04  1.96E-04  2.28E-04  1.11E-03", 
			"          1.34E-03  1.64E-03", " ", " SG11", "+        4.53E-05  6.21E-05 -4.89E-06  2.71E-05 -6.00E-05 -4.28E-05 -5.80E-05 -7.91E-05 -8.04E-05  8.12E-06 -4.10E-05 -1.00E-04", 
			"         -1.11E-04 -1.19E-04  5.96E-05", " ", "1", " ************************************************************************************************************************", 
			" ********************                                                                                ********************", 
			" ********************                               ITERATIVE TWO STAGE                              ********************", 
			" ********************                        CORRELATION MATRIX OF ESTIMATE (S)                      ********************", 
			" ********************                                                                                ********************", 
			" ************************************************************************************************************************", 
			" ", "", "            TH 1      TH 2      TH 3      TH 4      OM11      OM12      OM13      OM14      OM22      OM23      OM24      OM33", 
			"             OM34      OM44      SG11", " ", " TH 1", "+        1.00E+00", 
			" ", " TH 2", "+        1.62E-01  1.00E+00", " ", " TH 3", "+        1.21E-01  6.83E-02  1.00E+00", 
			" ", " TH 4", "+       -1.02E-02  6.06E-02  4.58E-01  1.00E+00", 
			" ", " OM11", "+       -1.84E-01  6.89E-02  3.86E-02 -5.49E-02  1.00E+00", 
			" ", " OM12", "+        1.29E-02  1.56E-01  2.43E-02 -1.07E-01  4.66E-01  1.00E+00", 
			" ", " OM13", "+       -1.11E-01 -8.87E-04 -1.19E-01  1.30E-01  1.51E-01  3.21E-01  1.00E+00", 
			" ", " OM14", "+       -1.40E-01 -1.58E-01  8.29E-02  1.18E-01  2.78E-01  2.85E-01  6.09E-01  1.00E+00", 
			" ", " OM22", "+       -2.39E-02 -1.16E-02 -9.22E-02 -6.00E-02  1.38E-01  2.94E-01  7.48E-02  1.45E-01  1.00E+00", 
			" ", " OM23", "+       -4.41E-02 -4.80E-02  2.60E-02  6.12E-02  1.70E-01  4.45E-02 -5.03E-02 -2.88E-02  2.11E-01  1.00E+00", 
			" ", " OM24", "+       -2.06E-01 -7.68E-02  7.98E-02  1.05E-01  1.46E-01  1.37E-01  2.60E-02  1.21E-01  3.98E-01  5.60E-01  1.00E+00", 
			" ", " OM33", "+       -1.09E-01 -1.03E-01 -4.71E-02  1.52E-01  1.19E-01  9.90E-02  3.85E-01  2.17E-01  2.42E-01  1.76E-01  2.29E-01  1.00E+00", 
			" ", " OM34", "+       -9.92E-03 -4.09E-02  8.96E-02  1.12E-01  1.93E-01  1.12E-01  2.01E-01  1.76E-01  2.42E-01  2.16E-01  2.71E-01  7.50E-01", 
			"          1.00E+00", " ", " OM44", "+        1.67E-02 -8.64E-03  7.43E-02 -1.34E-01  2.31E-01  1.35E-01  5.03E-02  1.39E-01  2.31E-01  1.26E-01  2.06E-01  4.22E-01", 
			"          7.32E-01  1.00E+00", " ", " SG11", "+        1.27E-01  1.64E-01 -9.73E-03  6.50E-02 -2.69E-01 -2.40E-01 -2.32E-01 -3.71E-01 -2.99E-01  2.76E-02 -1.94E-01 -2.01E-01", 
			"         -3.19E-01 -3.80E-01  1.00E+00", " ", "1", " ************************************************************************************************************************", 
			" ********************                                                                                ********************", 
			" ********************                               ITERATIVE TWO STAGE                              ********************", 
			" ********************                    INVERSE COVARIANCE MATRIX OF ESTIMATE (S)                   ********************", 
			" ********************                                                                                ********************", 
			" ************************************************************************************************************************", 
			" ", "", "            TH 1      TH 2      TH 3      TH 4      OM11      OM12      OM13      OM14      OM22      OM23      OM24      OM33", 
			"             OM34      OM44      SG11", " ", " TH 1", "+        5.64E+02", 
			" ", " TH 2", "+       -5.81E+01  4.92E+02", " ", " TH 3", "+       -5.58E+01 -1.06E+01  3.58E+02", 
			" ", " TH 4", "+        1.88E+01 -4.99E+01 -2.19E+02  5.51E+02", 
			" ", " OM11", "+        2.21E+02 -9.34E+01  1.15E+00  9.51E+00  1.82E+03", 
			" ", " OM12", "+       -1.55E+02 -1.74E+02 -1.34E+02  2.30E+02 -9.71E+02  3.08E+03", 
			" ", " OM13", "+        2.25E+01 -1.41E+02  2.11E+02 -1.51E+02  2.36E+02 -6.70E+02  2.11E+03", 
			" ", " OM14", "+        3.31E+01  2.38E+02 -1.60E+02 -3.63E+01 -3.83E+02  7.84E+00 -1.33E+03  2.61E+03", 
			" ", " OM22", "+       -6.43E+01 -3.31E+01  9.65E+01 -5.95E+00  1.16E+02 -4.50E+02  2.16E+02 -1.22E+02  1.18E+03", 
			" ", " OM23", "+       -8.28E+01  4.75E+01  2.11E+01 -8.60E-01 -2.77E+02  1.11E+02  1.08E+01  1.19E+02 -2.09E+01  1.11E+03", 
			" ", " OM24", "+        2.76E+02  9.48E+00 -6.66E+01 -8.42E+01  1.89E+02 -1.88E+02  1.39E+02 -1.42E+02 -4.74E+02 -8.76E+02  2.49E+03", 
			" ", " OM33", "+        6.55E+01  7.12E+01  4.88E+01 -3.43E+01  1.19E+01  5.27E+01 -3.83E+02  1.08E+02 -1.26E+02  5.83E-01 -5.26E+00  7.25E+02", 
			" ", " OM34", "+       -1.04E+02 -3.10E+01 -3.23E+01 -1.81E+02 -5.40E+01  1.44E+01  1.26E+02  3.20E+01  7.07E+01 -1.34E+02 -7.42E+01 -8.74E+02", 
			"          2.36E+03", " ", " OM44", "+       -2.01E+01 -5.44E+01 -8.13E+01  2.87E+02 -1.27E+02  8.51E+01  1.28E+02 -9.18E+01 -6.00E+01  6.08E+01 -4.86E+01  2.03E+02", 
			"         -1.30E+03  1.69E+03", " ", " SG11", "+       -2.14E+02 -5.78E+02  2.10E+01 -1.38E+02  7.71E+02  3.09E+02  4.95E+02  1.31E+03  1.00E+03 -9.24E+02  7.64E+02 -4.59E+02", 
			"          7.06E+02  1.06E+03  2.55E+04", " ", "1", " ", " ")
	# this attribute should be turned into the "method" field of the output list
	
	attr(testBlock, "method.name") <-  "Iterative Two Stage"
	processedBlock <- RNMImport:::.importMethodBlock(testBlock)
	
	# extract just final estimates

	finalEst <- processedBlock$FinalEstimates
	# method name obtained correctly
	checkEquals(processedBlock$method, "Iterative Two Stage", msg = " |method name passed down correctly." )
	# objective function value correct
	checkEquals(processedBlock$Objective.Final, -1120.307, msg = " | objective function is correct")
	# theta final estimates correct
	checkEquals(unname(finalEst$THETA), c(1.680, 1.590, 0.813, 2.370),
			 msg = " | theta final estimates correct")
	
	# omega finals correct
	
	checkEquals( finalEst$OMEGA, 
			structure(c(0.165, 0.00413, 0.00538, -0.0161, 0.00413, 0.133, 
							0.0172, 0.0123, 0.00538, 0.0172, 0.21, 0.0508, -0.0161, 0.0123, 
							0.0508, 0.161), .Dim = c(4L, 4L), .Dimnames = list(c("ETA1", 
									"ETA2", "ETA3", "ETA4"), c("ETA1", "ETA2", "ETA3", "ETA4"))),
			msg  = " |omega finals correct")
	
	# sigma finals correct
	checkEquals(finalEst$SIGMA, structure(0.0553, .Dim = c(1L, 1L), .Dimnames = list("EPS1", 
    "EPS1")), msg = " |sigma finals correct" )

	stdErr <- processedBlock$StandardError
	# theta standard error correct
	checkEquals( stdErr$THETA, structure(c(0.0461, 0.049, 0.0652, 0.0539), .Names = c("TH1", 
							"TH2", "TH3", "TH4")), 
			msg = " |theta standard error correct" )
	# omega standard error correct
	checkEquals( stdErr$OMEGA, structure(c(0.0289, 0.0231, 0.0324, 0.0276, 0.0231, 0.0348, 0.0382, 
		0.0273, 0.0324, 0.0382, 0.0649, 0.0451, 0.0276, 0.0273, 0.0451, 
		0.0405), .Dim = c(4L, 4L), .Dimnames = list(c("ETA1", "ETA2", 
		"ETA3", "ETA4"), c("ETA1", "ETA2", "ETA3", "ETA4"))),
		msg = " |omega standard error correct")

	checkEquals( stdErr$SIGMA, structure(0.00772, .Dim = c(1L, 1L), .Dimnames = list("EPS1", 
							"EPS1")),
			, msg = " |sigma standard error correct")
	# covariance matrices available and of correct dimension
	checkTrue(all( c(dim(processedBlock$CovarianceMatrix), dim(processedBlock$InverseCovarianceMatrix), dim(processedBlock$CorrelationMatrix)  ) == 15 ),
			msg  = " |covariance matrices available and of correct dimension" ) 
	
	# check that shrink values have been imported correctly
	
	checkEquals(processedBlock$ETAshrink,  c(3.3135, 19.596, 22.806, 14.537) )
	checkEquals(processedBlock$EPSshrink,  31.694 )
	
	# check that first line of objective fun termination value is available
	checkEquals(processedBlock$TermStatus, "OPTIMIZATION COMPLETED")
}
MangoTheCat/RNMImport documentation built on May 8, 2019, 4:36 p.m.