Carl Love

Carl Love

23646 Reputation

25 Badges

9 years, 266 days
Natick, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@Jno What is your Maple version (e.g., I'm using Maple 2022)?

@Jno 

I've updated the worksheet in my previous Reply. I think that it makes the math easier to understand, although the Maple coding is a bit trickier. In particular, I came up with a single simple unified formula for combining sums of squares and their degrees of freedom, which simplifies the presentation of SS_ASS_AP, and SSE. Please download the new worksheet.

The "error" (better termed the "unmodeled variance") is a factor of all the F-ratios, so it would have been impossible to proceed without it.

Maple's Vector and Matrix indices always start at 1. Array indices can start at any integer. I think that you're misinterpretting the DataFrame's column of row labels and row of column labels as being a 0th column and 0th row; they're not---they're just labels shown as a visual aid. They're akin to the tickmarks on a plot: a visual aid that's not part of the function being plotted.

Negative Vector and Matrix indices represent counting rows or columns backwards, i.e., from the bottom or right ends. Zero is never a valid Vector or Matrix index.

@AdeWaele What exactly do you find "not easily possible" about acer's Answer dgdx:=  D[1](g)??? It's the other two Answers that are flawed!

@Jno 

Re HFloat(...): That stands for "hardware float", which is the same as "double precision" in several other languages. It's correct, but unneeded information. I'm not sure why you're seeing those---perhaps  a display setting. Nonetheless:

  1. There's no reason that you need to see them.
  2. They can be ignored.
  3. The following program update removes them anyway.

Re accessing/extracting the values: I've changed the output format to a DataFrame (very similar to a Matrix) that presents a traditional ANOVA table. At the bottom of the following worksheet, I show how to extract/access the values.

I extended the procedure to do the complete two-way ANOVA with F-ratios and their p-values.

restart
:

ANOVA_2way:= proc(X::And(Array, 3 &under ArrayNumDims))
description `Two-factor analysis of variance (aka, two-way ANOVA)`;
option `Author: Carl Love <carl.j.love@gmail.com> 2022-Jul-1`;
uses St= Statistics;
local
    AAD:= (A::rtable, d::posint)-> #replacement for ArrayTools:-AddAlongDimension
    local k;
        add(A[(..)$d-1, k], k= [ArrayDims](A)[d]),

    N:= numelems(X), #total number of observations
    Xij:= AAD(X, 3), #sums of trials  

    #sum-of-squares procedures:
    SS:= (A::rtable)-> local n:= numelems(A); (add(A^~2)*n/N, n),
    all:= SS(<add(Xij)>)[1], #ie, Sum(X)^2/N,
    SSrec:= (SS::realcons, n::And(posint, Not(1)))->
        Record("df"= n-1, ("SS","MS")=~ (SS-all)/~(1, n-1)),
    `&-`:= (R::record, S::record)->
        SSrec(R:-SS - S:-SS + all, R:-df - S:-df + 1),

    #sums of squares due to...
    SS_P:= SSrec(SS(AAD(Xij, 2))), #...partition ("P factor")
    SS_A:= SSrec(SS(AAD(Xij, 1))), #...treatment ("A factor")
    SS_AP:= SSrec(SS(Xij)) &- SS_P &- SS_A, #...interaction (P x A)
    TSS:= SSrec(SS(X)), #...total, ie, Sum(X^~2) - Sum(X)^2/N
    SSE:= TSS &- SS_P &- SS_A &- SS_AP, #...random or unaccounted factors {"error")

    #procedure for each row of output (with F-ratio and its p-value):
    F:= (SS::record)->
    local F:= SS:-MS/SSE:-MS;
        <
            SS:-SS | SS:-df | SS:-MS | F |
            1 - St:-CDF('FRatio'(SS:-df, SSE:-df), F, 'numeric')
        >,

    Out:= <F~([SS_P, SS_A, SS_AP, SSE, TSS])[]> #ANOVA table
;
    #Put blanks for N/A entries at bottom right:
    Out[-2, -2..]:= ``;  Out[-1, -3..]:= ``;
    DataFrame(
        #Change HFloats and round to 6 sig. digits:
        subsindets(Out, float, evalf[6]),         
        'rows'= ["P", "A", "PxA", "Error", "Total"],
        'columns'= ["SS", "df", "MSS", "F", "p-val"]
    )
end proc
:

#Example usage:

#Appraiser (A) - Part (P) data with 3 measurements for each (A,P) pair:
AP:= Array(
    [
         [[29, 41, 64],    [8,   25,  7],    [4, -11, -15]],
        -[[56, 68, 58],    [47, 122, 68],    [138, 113, 96]],
         [[134, 117, 127], [119, 94, 134],   [88, 109, 67]],
         [[47, 50, 64],    [1, 103, 20],     [14, 20, 11]],
        -[[80, 92, 84],    [56, 120, 128],   [146, 107, 145]],
         [[2, -11, -21],   [-20, 22, 6],    -[29, 67, 49]],
         [[59, 75, 66],    [47, 55, 83],     [2, 1, 21]],
         [-[31, 20, 17],   [-63, 8,- 34],   -[46, 56, 49]],
         [[226, 199, 201], [180, 212, 219],  [177, 145, 187]],
        -[[136, 125, 131], [168, 162, 150],  [149, 177, 216]]
    ]/100,
    datatype= hfloat
):

R:= ANOVA_2way(AP);

"DataFrame([[[88.3619,9,9.81799,213.517,0.],[3.16726,2,1.58363,34.4401,1.09385 10^(-10)],[0.358982,18,0.0199435,0.433721,0.974106],[2.75893,60,0.0459822,,],[94.6471,89,,,]]],rows=["P","A","PxA","Error","Total"],columns=["SS","df","MSS","F","p-val"])"

#Access/extract the values like this:
R["A", "SS"];
R["PxA", "p-val"];

3.16726

.974106

#...or matrix style, like this:
R[2,1];
R[3,5];

3.16726

.974106

 

Download ANOVA.mw

@C_R Hmm, that link worked for me earlier, but it doesn't anymore. It was a 3-4-page paper with 2 examples of solving a 7th-order bivariate PDE by the Variational Iteration Method. The examples were done in Maple, but the commands weren't explicitly shown.

@Jno You don't have any Array named TestA. If TestA existed, then if it caused an error, the error message would list the contents of TestA rather than the name "TestA".

The command restart erases all variables from the "active" memory. Thus, if you do something like the following, you'll get that error:

TestA:= Array([1]);
restart;
ArrayDims(TestA);

A correct order is

restart;
TestA:= Array([1]);
ArrayDims(TestA);

@mehran rajabi Please post an executed worksheet that shows the blank space where the plot should be.

@Jno My guess is that you declared nm1 as an array rather than as an Array. The lowercase array has been obsolete for about 20 years. It only still exists so that old code will still run. Likewise, vector and matrix are obsolete.

Arrays, Vectors, and Matrixes are collectively called rtables. My line of code that you asked about specifies that the procedure ANOVA_3D has one parameter, X, whose corresponding passed argument must be both an rtable And have exactly 3 dimensions.

@Christian Wolinski Note the trick that acer used to bypass the plots command's overzealous enforcement of integer-only thickness.

@acer That's very nice, acer! I wish there was an easier way to get black backgrounds.

Is the entire coloring of the curve due to you removing color specs from the plot structure? 

@mehran rajabi What if you try Christian Wolinski's version of the code?

Other than the thickness= 0.1, I'm not aware of anything in my code above that wouldn't work in Maple 2018.

Can you make any 3D plots at all? Sometimes something needs to be adjusted in your operating system for that. For example; try

plot3d(x^2+y^2, x= -1..1, y= -1..1);

Here's a better workaround for that bug:

select[indices](()-> A[args], B);

@Jno You wrote:

  • I tried putting it in a blank Maple worksheet, but I get an error: "Error, invalid arrow procedure." 

I guess that you didn't first use Ctrl-J or Ctrl-K; thus it was still in 2D-Input mode.

You can also download this worksheet. As you can see, my computations produce the same values as shown in your MSA manual. 
 

restart
:

ANOVA_3D:= (X::And(rtable, 3 &under rtable_num_dims))->
local
    N:= numelems(X),
    #replacement for ArrayTools:-AddAlongDimension:
    AAD:= (A,d)-> local k; add(A[(..)$d-1, k], k= [rtable_dims](A)[d]),
    SS:= A-> add(A^~2)*numelems(A)/N,
    Xij:= AAD(X, 3),  Xi:= AAD(Xij, 2),  
    all:= SS(<add(Xi)>), SSi:= SS(Xi),  SSj:= SS(AAD(Xij, 1))    
;
    Record(
        ("SS__P", "SS__A", "TSS")=~ (SSi, SSj, SS(X)) -~ all,
        "SS__AP"= SS(Xij) - SSi - SSj + all
    )
:

#Appraiser (A) - Part (P) data with 3 measurements for each (A,P) pair:
AP:= Array(
    [
         [[29, 41, 64],    [8,   25,  7],   [4, -11, -15]],
        -[[56, 68, 58],    [47, 122, 68],   [138, 113, 96]],
         [[134, 117, 127], [119, 94, 134],  [88, 109, 67]],
         [[47, 50, 64],    [1, 103, 20],    [14, 20, 11]],
        -[[80, 92, 84],    [56, 120, 128],  [146, 107, 145]],
         [[2, -11, -21],   [-20, 22, 6],   -[29, 67, 49]],
         [[59, 75, 66],    [47, 55, 83],    [2, 1, 21]],
         [-[31, 20, 17],   [-63, 8,- 34],  -[46, 56, 49]],
         [[226, 199, 201], [180, 212, 219], [177, 145, 187]],
        -[[136, 125, 131], [168, 162, 150], [149, 177, 216]]
    ]/100,
    datatype= hfloat
):

ANOVA_3D(AP);

Record(SS__P = HFloat(88.36193444444446), SS__A = HFloat(3.1672622222222215), TSS = HFloat(94.64711222222219), SS__AP = HFloat(0.35898222222218))

 

 

Download ANOVA.mw

 

@planetmknzm 

1. Please explain in words what the code does.

2. Is it just a tangent plane and TNB frame moving along a space curve embedded on a sphere?

3. If you want me to help, first make a substantially simpler version of the same thing, 1/4 the current code size or less.

4. I have no interest in improving some lengthy code AND THEN trying to make those improvements work in Maple 12 when the earliest Maple that I have access to is Maple 2019. You'll need to get a more-recent version for me to work on it.

5. Don't email me; use MaplePrimes.

@Jno Here's another version, nearly twice as fast (not that the previous version was slow by any means), and more simply coded. I originally used ArrayTools:-AddAlongDimension because I mistakenly thought that all the commands is ArrayTools used external compiled code for efficiency. But AddAlongDimension is a 57-line Maple procedure, which is quite surprising because I replaced it with a 1-line procedure using only top-level built-in commands:

AAD:= (A,d)-> local k; add(A[(..)$d-1, k], k= [rtable_dims](A)[d])

It seemed worthwhile to make the change because that one-liner is so glaringly simple. So, here's the new code:

ANOVA_3D:= (X::And(rtable, 3 &under rtable_num_dims))-> 
local
    N:= numelems(X),
    #replacement for ArrayTools:-AddAlongDimension:
    AAD:= (A,d)-> local k; add(A[(..)$d-1, k], k= [rtable_dims](A)[d]),
    SS:= A-> add(A^~2)*numelems(A)/N,
    Xij:= AAD(X, 3),  Xi:= AAD(Xij, 2),  
    all:= SS(<add(Xi)>), SSi:= SS(Xi),  SSj:= SS(AAD(Xij, 1))    
; 
    Record(
        ("SS__P", "SS__A", "TSS")=~ (SSi, SSj, SS(X)) -~ all,
        "SS__AP"= SS(Xij) - SSi - SSj + all
    )
:

The example usage, calling protocol, etc., are identical.

From anywhere in a Maple worksheet, type Ctrl-J or Ctrl-K. This should give you a prompt for 1D input:

If you copy-and-paste my code to the space in front of that prompt and press Enter, it should work.

Regarding those vectors of measurements that you mentioned earlier: Do they all have the same number of measurements? If they don't, then I think that you will need to use a (2-D) matrix of vectors like you were mentioning. It's not a big deal to make that change. 
 

1 2 3 4 5 6 7 Last Page 1 of 621