R Under development (unstable) (2024-07-17 r86903 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > ### test the extended interpreter for the left hand side of linear hypotheses > ### Features: > ### - fully recursive expression parser built upon a small code fragment copied over from base::codetools > ### - the parser stops if any of the following conditions is not met: > ### - any variable must be addressed only once > ### - all operators and functions must finally evaluate to a a real valued literal > ### - function parameters must not denote an effect name > ### - effects can not be multiplied or divided by another effect > ### - additive or subtractive terms involving an effect and a numeric > ### constants must not be specified > ### - coefficients associated with named effects must not evaluate to zero > ### > ### Examples: > ### x1 + x1 == 0 -> not accepted > ### x1 + x2 -1 == 0 -> not accepted > ### x1 * x2 == 0 -> not accepted > ### x1 / x2 == 0 -> not accepted > ### f(x1) == 0 -> not accepted if x1 denotes an effect > ### 2*3 == 6 -> not accepted because no effect was named > ### x1 + x2*0 == 0 -> not accepted because this is likely an oversight > ### x1 + 3*(4-5+1)*x2 == 0 -> not accepted because this is likely an oversight > ### x1*3/0 == 0 -> not accepted because coefficient would become infinite > ### log(-1)*x1 == 0 -> not accepted, because the result is not finite > ### x1 + x2 +0 == 0 -> accepted because adding zero does not make a difference > ### sin(pi/2) * x1 == 0 -> accepted if 'pi' is not an effect > ### sin(Pi/2) * x1 == 0 -> accepted if 'Pi' is not an effect. However, if the environment does not define Pi the evaluation may still fail. > > > tmp <- multcomp:::chrlinfct2matrix( c( l01 = " x1 - x2 = 2" + , l02 = " x2 + 3 * x3 = 1" + , l03 = " (x1 - x2) - (x3 - x4) = 0" + , l04 = "+(x1 - x2)*-2 - (1/3+2)*( +x3 - 2*x4 ) = -1" + , l05 = "-(x1 - x2)*-2 - (1/3+2)*( -x3 - 2*x4 ) = -2" + , l06 = "-(x1 - x2)*-2 - (1/3+2)*( -x3 - 2*x4 )*7/-10 = -3" + , l07 = "-1*(x1:x2 - x1:x2:x3) - x3 = -4" + , l08 = "-(x1:x2 - x1:x2:x3) - x3 = -4" + , l09 = "-(x1:x2 - 3*x1:x2:x3)*-2 - x3 -5/3*-x4= -5" + , l10 = "--cos(pi/2)*x1 - 10*(log(10^-3)+1)*-x2 -10^-3*x3 + -exp(-2)*x4= -6" + , l11 = " x1 + x2 + 0 = -7" + ), c('x1','x2','x3','x4','x1:x2','x1:x2:x3') ) > > stopifnot(max(abs( dK <- tmp$K - + rbind( c( 1, -1, 0, 0, 0, 0 ) + , c( 0, 1, 3, 0, 0, 0 ) + , c( 1, -1, -1, 1, 0, 0 ) + , c( -2, 2, -(1/3+2), 2*(1/3+2), 0, 0 ) + , c( 2, -2, (1/3+2), 2*(1/3+2), 0, 0 ) + , c( 2, -2, (1/3+2)*-7/10, 2*(1/3+2)*-7/10, 0, 0 ) + , c( 0, 0, -1, 0, -1, 1 ) + , c( 0, 0, -1, 0, -1, 1 ) + , c( 0, 0, -1, -5/3*-1, 2, -6 ) + , c( --cos(pi/2), 10*(log(10^-3)+1), -10^-3, -exp(-2), 0, 0 ) + , c( 1, 1, 0, 0, 0, 0 ) + ))) < sqrt(.Machine$double.eps)) > > stopifnot(max(abs( tmp$m - + c( 2 + , 1 + , 0 + , -1 + , -2 + , -3 + , -4 + , -4 + , -5 + , -6 + , -7 + ))) < sqrt(.Machine$double.eps)) > > expectFail <- function(testname, x) { + if ( class(x) != 'try-error' ) { + stop(testname, ' unexpectedly succeeded. Result is: ', paste(x, collapse = ', '),'\n') + } + message(testname, ' expectedly failed. Message is: ', attr(x,'condition')$message, '\n') + } > > expectSucc <- function(testname, x,expected) { + if ( class(x) == 'try-error' ) { + stop(testname, ' unexpectedly failed. Message is: ', attr(x,'condition')$message, '\n') + } + message(testname, ' expectedly succeeded.', + ' Expected result is: ', paste(x, collapse = ', '), ', ', + ' actual result is: ', paste(x, collapse = ', '), '\n') + + stopifnot(all.equal(as.vector(x$K),expected$K)) + stopifnot(all.equal(as.vector(x$m),expected$m)) + stopifnot(all(as.vector(x$alternative) %in% expected$alternative)) + } > > expectFail('test 01', try( multcomp:::chrlinfct2matrix( c('x1 - x1 = 0'), c('x1','x2')), silent = T)) test 01 expectedly failed. Message is: multcomp:::expression2coef::walkCode::sub: multiple occurence of 'x1' found within expression 'x1 - x1' > > expectFail('test 02', try( multcomp:::chrlinfct2matrix( c('x1 - X2 = 0'), c('x1','x2')), silent = T)) test 02 expectedly failed. Message is: multcomp:::chrlinfct2matrix: variable(s) 'X2' not found > > expectFail('test 03', try( multcomp:::chrlinfct2matrix( c('x1 - x2 -1 = 0'), c('x1','x2')), silent = T)) test 03 expectedly failed. Message is: multcomp:::expression2coef::walkCode::sub: forming a difference between a constant and an effect as in 'x1 - x2 - 1' is not supported > > expectFail('test 04', try( multcomp:::chrlinfct2matrix( c('x1 * x2 = 0'), c('x1','x2')), silent = T)) test 04 expectedly failed. Message is: multcomp:::expression2coef::walkCode::mul: the multiplication of effects 'x1', 'x2' as in 'x1 * x2' is not supported > > expectFail('test 05', try( multcomp:::chrlinfct2matrix( c('x1 / x2 = 0'), c('x1','x2')), silent = T)) test 05 expectedly failed. Message is: multcomp:::expression2coef::walkCode::div: cant't divide by effect 'x2' in 'x1/x2' > > expectFail('test 06', try( multcomp:::chrlinfct2matrix( c('x1 - exp(x2) = 0'), c('x1','x2')), silent = T)) test 06 expectedly failed. Message is: multcomp:::expression2coef::walkCode::eval: within 'exp(x2)', the term 'x2' must not denote an effect. Apart from that, the term must evaluate to a real valued constant > > expectFail('test 07', try( multcomp:::chrlinfct2matrix( c('sin(Pi)*x1 = 0'), c('x1','x2')), silent = T)) test 07 expectedly failed. Message is: multcomp:::expression2coef::walkCode::eval: the evaluation of the expression 'sin(Pi)' failed with "object 'Pi' not found" > > expectFail('test 08', try( multcomp:::chrlinfct2matrix( c('3*4 = 0'), c('x1','x2')), silent = T)) test 08 expectedly failed. Message is: multcomp:::expression2coef: The lhs expression '3 * 4' contains a numeric offset term evaluating to 12. This is either an internal error or a misspecification from your part. If so, please pull these offsets to the right-hand side of the equation > > expectFail('test 09', try( multcomp:::chrlinfct2matrix( c('x1 + 3*(4-5+1)*x2 = 0'), c('x1','x2')), silent = T)) test 09 expectedly failed. Message is: multcomp:::expression2coef::walkCode::mul: The constant part of the expression '3 * (4 - 5 + 1) * x2' evaluates to zero. This would zero out the effect(s) 'x2' > > expectFail('test 10', try( multcomp:::chrlinfct2matrix( c('x1*3/0 = 0'), c('x1','x2')), silent = T)) test 10 expectedly failed. Message is: multcomp:::expression2coef::walkCode::div: can't divide by '0' in 'x1 * 3/0' > > expectFail('test 11', try( multcomp:::chrlinfct2matrix( c('log(-1)*x1 = 0'), c('x1','x2')), silent = T)) test 11 expectedly failed. Message is: multcomp:::expression2coef::walkCode::eval: the expression 'log(-1)' did not evaluate to a real valued constant. Result is 'NaN' Warning message: In log(-1 * 1) : NaNs produced > > expectSucc('test 12', try( multcomp:::chrlinfct2matrix( c('x1 -x2 -1/2*(-x2:x3 + x4:x5) = 0'), + c( 'x1', 'x2', 'x3', 'x4', 'x5', 'x2:x3','x4:x5')), silent = T), + expected = list( K = c( 1, -1, 0, 0, 0, 1/2, -1/2), + m = 0, alternative = 'two.sided')) test 12 expectedly succeeded. Expected result is: c(1, -1, 0, 0, 0, 0.5, -0.5), 0, two.sided, actual result is: c(1, -1, 0, 0, 0, 0.5, -0.5), 0, two.sided > > expectSucc('test 13', try( multcomp:::chrlinfct2matrix( c( 'x1 -x2 -1/2*(--x2:x3 + x4:x5) = 0'), + c( 'x1', 'x2', 'x3', 'x4', 'x5', 'x2:x3','x4:x5')), silent = T), + expected = list( K = c( 1, -1, 0, 0, 0, -1/2, -1/2), + m = 0, alternative = 'two.sided')) test 13 expectedly succeeded. Expected result is: c(1, -1, 0, 0, 0, -0.5, -0.5), 0, two.sided, actual result is: c(1, -1, 0, 0, 0, -0.5, -0.5), 0, two.sided > > expectSucc('test 14', try( multcomp:::chrlinfct2matrix( c( 'x1 -x2 -1/2*(`-x2:x3` + x4:x5) = 0'), + c( 'x1', 'x2', 'x3', 'x4', 'x5', '-x2:x3','x4:x5')), silent = T), + expected = list( K = c( 1, -1, 0, 0, 0, -1/2, -1/2), + m = 0, alternative = 'two.sided')) test 14 expectedly succeeded. Expected result is: c(1, -1, 0, 0, 0, -0.5, -0.5), 0, two.sided, actual result is: c(1, -1, 0, 0, 0, -0.5, -0.5), 0, two.sided > > expectSucc('test 15', try( multcomp:::chrlinfct2matrix( c( 'x1 -x2 -1/2*(-(x2:x3) + x4:x5) = 0'), + c( 'x1', 'x2', 'x3', 'x4', 'x5', 'x2:x3','x4:x5')), silent = T), + expected = list( K = c( 1, -1, 0, 0, 0, 1/2, -1/2), + m = 0, alternative = 'two.sided')) test 15 expectedly succeeded. Expected result is: c(1, -1, 0, 0, 0, 0.5, -0.5), 0, two.sided, actual result is: c(1, -1, 0, 0, 0, 0.5, -0.5), 0, two.sided > > expectSucc('test 16', try( multcomp:::chrlinfct2matrix( c( 'x1 -x2 -1/2*(-1*x2:x3 + x4:x5) = 0'), + c( 'x1', 'x2', 'x3', 'x4', 'x5', 'x2:x3','x4:x5')), silent = T), + expected = list( K = c( 1, -1, 0, 0, 0, 1/2, -1/2), + m = 0, alternative = 'two.sided')) test 16 expectedly succeeded. Expected result is: c(1, -1, 0, 0, 0, 0.5, -0.5), 0, two.sided, actual result is: c(1, -1, 0, 0, 0, 0.5, -0.5), 0, two.sided > > > > expectSucc('test 17', try( multcomp:::chrlinfct2matrix( c( 'x1 -x2 -1/2*(+-+--x2:x3:x4 + x4:x5) = 0'), + c( 'x1', 'x2', 'x3', 'x4', 'x5', 'x2:x3:x4','x4:x5')), silent = T), + expected = list( K = c( 1, -1, 0, 0, 0, 1/2, -1/2), + m = 0, alternative = 'two.sided')) test 17 expectedly succeeded. Expected result is: c(1, -1, 0, 0, 0, 0.5, -0.5), 0, two.sided, actual result is: c(1, -1, 0, 0, 0, 0.5, -0.5), 0, two.sided > > expectFail('test 18', try( multcomp:::chrlinfct2matrix( c( 'x1 - x2 - 1/2 * ( x2:-x3 + x4:x5 ) = 0'), + c( 'x1','x2','x2:x3','x4:x5')), silent = T)) test 18 expectedly failed. Message is: multcomp:::chrlinfct2matrix: variable(s) 'x2:-x3' not found > > > > proc.time() user system elapsed 1.21 0.18 1.39