How can i solve some AMPL Syntax problem with my code? - syntax

I'm learnig to use AMPL to solve some linear programing related problems; but i have a syntax error with a part of my code, and i dont know how to solve it.
#Archivo Mod
#Conjuntos
set T; #Conjunto Periodos
set I; #Conjunto Plantas
set J; #Conjunto Clientes
set M; #Conjunto de materias primas
#Parametros
param D{j in J, t in T};
param CAM{i in I, m in M};
param CFP{i in I, t in T};
param CVP{i in I, t in T};
param CFI{i in I, t in T};
param CVI{i in I, t in T};
param QP{i in I};
param QI{i in I};
param CT{i in I, j in J};
param R{i in I, m in M};
param L; #Gran M
#Variables de desiciòn
var X{m in M, i in I, t in T}>=0 integer;
var Y{i in I, t in T}>=0 integer;
var H{i in I, t in T}>=0 integer; #Cambie la varible I de notación para no confundirla con el conjunto
var Z{i in I, t in T} binary;
var CI{i in I, t in T} binary;
var W{i in I, j in J, t in T}>=0 integer;
var TR{i in I, j in J, t in T} binary; #Si se transporta o no
#F.O
minimize FO: sum{i in I,t in T}CFP[i,t]*Z[i,t]+sum{i in I,t in T}CFI[i,t]*CI[i,t]+sum{i in I, m in M, t in T}CAM[i,m]*X[m,i,t]+sum{i in I, j in J, t in T}TR[i,j,t]*CT[i,J]+sum{i in I,t in T}CvI[i,t]*H[i,t]+sum{i in I,t in T}CVP[i,t]*Y[i,t];
#Restricciones
s.t. R1{i in I, t in T}: Y[i,t]<=QP[i];
s.t. R2{i in I, t in T}: H[i,t]<=QI[i];
s.t. R3{i in I, t in T}: Y[i,t]<=M*Z[i,t];
s.t. R4{i in I, t in T}: H[i,t]<=M*CI[i,t];
s.t. R5{i in I}: H[i,0]=0;
s.t. R6{j in J,t in T}: sum{i in I}W[i,j,t]=D[j,t];
s.t. R7{i in I, t in {1,2,3,4,5}}:H[i,t-1]+Y[i,t]=H[i,t]+sum{j in J}W[i,j,t];
s.t. R8{i in I}:H[i,5]+Y[i,6]=H[i,6]+sum{j in J}W[i,j,6];
s.t. R9{j in J, t in T}:D[j,t]=sum{i in I}W[i,j,t];
s.t. R10{i in I, m in M, t in T}: R[i,m]*Y{i,t]=X[m,i,t];
With the next Data File
#Archivo DAT
#Definiciòn de conjuntos (Se puede separar con comas o espacios)
set T:=1,2,3,4,5,6; #Conjunto Periodos
set I:= P1, P2, P3; #Conjunto Plantas
set J:= C1, C2, C3, C4; #Conjunto Clientes
set M:= M1, M2, M3; #Conjunto de materias primas
#Parametros
param D:
1 2 3 4 5 6:=
C1 300 350 330 320 360 350
C2 500 600 550 400 450 500
C3 1000 800 850 900 950 850
C4 450 600 500 550 400 490
;
param CAM:
M1 M2 M3:=
P1 50 70 20
P2 30 100 20
P3 30 50 20
;
param R:
M1 M2 M3:=
P1 2 1 3
P2 3 1 5
P3 2 1 1
;
param QP:=
P1 1000
P2 800
P3 800
;
param QI:=
P1 300
P2 400
P3 350
;
param CFP:
1 2 3 4 5 6:=
P1 10 10 12 15 15 13
P2 12 12 15 17 17 13
P3 25 25 20 30 30 25
;
param CVP:
1 2 3 4 5 6:=
P1 5 5 10 8 8 7
P2 6 6 12 8 9 8
P3 13 13 10 15 15 15
;
param CFI:
1 2 3 4 5 6:=
P1 2 3 2 2 3 2
P2 2 3 2 2 3 2
P3 2 5 2 7 9 7
;
param CVI:
1 2 3 4 5 6:=
P1 1 1 1 2 1 2
P2 2 2 3 2 2 2
P3 2 2 1 2 1 3
;
param CT:
C1 C2 C3 C4:=
P1 100 80 30 100
P2 120 30 30 120
P3 90 70 30 150
;
param L:=10000000
But when i run the mod file, i have this error
Taller1.mod, line 36 (offset 981):
syntax error
context: minimize FO: sum{i in I,t in T}CFP[i,t]*Z[i,t]+sum{i in I,t in T}CFI[i,t]*CI[i,t]+sum{i in I, m in M, t in T}CAM[i,m]*X[m,i,t]+sum{i in I, j in J, t in >>> T}TR[i,j,t]*CT[i,J] <<< +sum{i in I,t in T}CvI[i,t]*H[i,t]+sum{i in I,t in T}CVP[i,t]*Y[i,t];
I checked my code, but I don't understand what the error is. Please! Help me.

CT[i,>>>J<<<<]

Related

How to write a program for multiplication table PL/SQL ask the user input a number

How to write a program for multiplication table PL/SQL ask the user input a number
this is the code display just table without input
Declare
i NUMBER:=0;
x NUMBER;
Begin
loop
i := i+1;
x :=2*i;
dbms_output.put_line('2'||'x'||i||'='||x);
IF i >=10 THEN
EXIT ;
END IF;
END loop;
END;
/
A simple option (ran in SQL*Plus) is
SQL> set ver off
SQL> begin
2 for i in 1 .. 10 loop
3 dbms_output.put_line(&&par_number|| ' x ' || i ||' = '|| &&par_number * i);
4 end loop;
5 end;
6 /
Enter value for par_number: 2
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
PL/SQL procedure successfully completed.
SQL>
As Koen commented, depending on a client, substitution variable (&&par_number) might need to be modified to a bind variable (:par_number), or you'd enter it into a page item, or some other option.
More info you provide, better answer you get.
You could create the complete multiplication table with just sql and then select whatever you want without using PL/SQL ...
Here is the code: (all ran in SQL Developer)
WITH
nums AS
(
Select LEVEL "N" From Dual Connect By LEVEL <= 10
),
tbl AS
(
Select COL_N * N1 "N1", COL_N * N2 "N2", COL_N * N3 "N3", COL_N * N4 "N4", COL_N * N5 "N5",
COL_N * N6 "N6", COL_N * N7 "N7", COL_N * N8 "N8", COL_N * N9 "N9", COL_N * N10 "N10"
From ( SELECT n2.N "COL_N",n1. N "N", n1.N "ROW_N"
FROM nums n1
INNER JOIN nums n2 ON(1 = 1)
)
PIVOT ( MAX(N) FOR ROW_N IN(1 "N1", 2 "N2", 3 "N3", 4 "N4", 5 "N5", 6 "N6", 7 "N7", 8 "N8", 9 "N9", 10 "N10") )
Order By COL_N
)
... the content is
SELECT N1 "1", N2 "2", N3 "3", N4 "4", N5 "5", N6 "6", N7 "7", N8 "8", N9 "9", N10 "10" FROM tbl
1 2 3 4 5 6 7 8 9 10
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
... and select with variable passed to:
SELECT LPAD(N1, 2, ' ') || ' x ' || LPAD(&&M_NUM, 2, ' ') || ' = ' || LPAD(N1 * &&M_NUM, 3, ' ') "RESULTS"
FROM tbl
-- with &&M_NUM = 6 results:
RESULTS
--------------
1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
4 x 6 = 24
5 x 6 = 30
6 x 6 = 36
7 x 6 = 42
8 x 6 = 48
9 x 6 = 54
10 x 6 = 60
... Or you could get the results in one row (it's 6 again)
SELECT * FROM tbl WHERE N1 = &&M_NUM
N1 N2 N3 N4 N5 N6 N7 N8 N9 N10
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
6 12 18 24 30 36 42 48 54 60
... or in columns ...
SELECT N1, &&M_NUM "MULTIPLYED _BY",
CASE &&M_NUM WHEN 2 THEN N2
WHEN 3 THEN N3
WHEN 4 THEN N4
WHEN 5 THEN N5
WHEN 6 THEN N6
WHEN 7 THEN N7
WHEN 8 THEN N8
WHEN 9 THEN N9
WHEN 10 THEN N10
END "RESULT"
FROM tbl
N1 MULTIPLYED _BY RESULT
---------- -------------- ----------
1 6 6
2 6 12
3 6 18
4 6 24
5 6 30
6 6 36
7 6 42
8 6 48
9 6 54
10 6 60
... or anything else you like...

Algorithm for visiting all grid cells in pseudo-random order that has a guaranteed uniformity at any stage

Context:
I have a hydraulic erosion algorithm that needs to receive an array of droplet starting positions. I also already have a pattern replicating algorithm, so I only need a good pattern to replicate.
The Requirements:
I need an algorism that produces a set of n^2 entries in a set of format (x,y) or [index] that describe cells in an nxn grid (where n = 2^i where i is any positive integer).
(as a set it means that every cell is mentioned in exactly one entry)
The pattern [created by the algorism ] should contain zero to none clustering of "visited" cells at any stage.
The cell (0,0) is as close to (n-1,n-1) as to (1,1), this relates to the definition of clustering
Note
I was/am trying to find solutions through fractal-like patterns built through recursion, but at the time of writing this, my solution is a lookup table of a checkerboard pattern(list of black cells + list of white cells) (which is bad, but yields fewer artifacts than an ordered list)
C, C++, C#, Java implementations (if any) are preferred
You can use a linear congruential generator to create an even distribution across your n×n space. For example, if you have a 64×64 grid, using a stride of 47 will create the pattern on the left below. (Run on jsbin) The cells are visited from light to dark.
That pattern does not cluster, but it is rather uniform. It uses a simple row-wide transformation where
k = (k + 47) mod (n * n)
x = k mod n
y = k div n
You can add a bit of randomness by making k the index of a space-filling curve such as the Hilbert curve. This will yield the pattern on the right. (Run on jsbin)
     
     
You can see the code in the jsbin links.
I have solved the problem myself and just sharing my solution:
here are my outputs for the i between 0 and 3:
power: 0
ordering:
0
matrix visit order:
0
power: 1
ordering:
0 3 2 1
matrix visit order:
0 3
2 1
power: 2
ordering:
0 10 8 2 5 15 13 7 4 14 12 6 1 11 9 3
matrix visit order:
0 12 3 15
8 4 11 7
2 14 1 13
10 6 9 5
power: 3
ordering:
0 36 32 4 18 54 50 22 16 52 48 20 2 38 34 6
9 45 41 13 27 63 59 31 25 61 57 29 11 47 43 15
8 44 40 12 26 62 58 30 24 60 56 28 10 46 42 14
1 37 33 5 19 55 51 23 17 53 49 21 3 39 35 7
matrix visit order:
0 48 12 60 3 51 15 63
32 16 44 28 35 19 47 31
8 56 4 52 11 59 7 55
40 24 36 20 43 27 39 23
2 50 14 62 1 49 13 61
34 18 46 30 33 17 45 29
10 58 6 54 9 57 5 53
42 26 38 22 41 25 37 21
the code:
public static int[] GetPattern(int power, int maxReturnSize = int.MaxValue)
{
int sideLength = 1 << power;
int cellsNumber = sideLength * sideLength;
int[] ret = new int[cellsNumber];
for ( int i = 0 ; i < cellsNumber && i < maxReturnSize ; i++ ) {
// this loop's body can be used for per-request computation
int x = 0;
int y = 0;
for ( int p = power - 1 ; p >= 0 ; p-- ) {
int temp = (i >> (p * 2)) % 4; //2 bits of the index starting from the begining
int a = temp % 2; // the first bit
int b = temp >> 1; // the second bit
x += a << power - 1 - p;
y += (a ^ b) << power - 1 - p;// ^ is XOR
// 00=>(0,0), 01 =>(1,1) 10 =>(0,1) 11 =>(1,0) scaled to 2^p where 0<=p
}
//to index
int index = y * sideLength + x;
ret[i] = index;
}
return ret;
}
I do admit that somewhere along the way the values got transposed, but it does not matter because of how it works.
After doing some optimization I came up with this loop body:
int x = 0;
int y = 0;
for ( int p = 0 ; p < power ; p++ ) {
int temp = ( i >> ( p * 2 ) ) & 3;
int a = temp & 1;
int b = temp >> 1;
x = ( x << 1 ) | a;
y = ( y << 1 ) | ( a ^ b );
}
int index = y * sideLength + x;
(the code assumes that c# optimizer, IL2CPP, and CPP compiler will optimize variables temp, a, b out)

Is there a function to generate a specific n Multichoose r combination, given the index number?

For example, 3 multichoose 2 has the following combinations:
i combo
0 = [0,0]
1 = [0,1]
2 = [0,2]
3 = [1,1]
4 = [1,2]
5 = [2,2]
Could a function be written whose arguments are n,r,i and returns the combination in question, without iterating through every combination before it?
Could a function be written whose arguments are n,r,i and returns the combination in question, without iterating through every combination before it?
Yes. We have to do a little counting to get at the heart of this problem. To better illustrate how this can be broken down into very simple smaller problems, we will look at a larger example. Consider all combinations of 5 chosen 3 at a time with no repeats (we will say from here on out 5 choose 3).
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 4
[3,] 1 2 5
[4,] 1 3 4
[5,] 1 3 5
[6,] 1 4 5
[7,] 2 3 4
[8,] 2 3 5
[9,] 2 4 5
[10,] 3 4 5
Notice the first 6 rows. If we remove the first column of these 6 rows and subtract 1 from every element, we obtain:
[,1] [,2] [,1] [,2]
[1,] 2 3 [1,] 1 2
[2,] 2 4 subtract 1 [2,] 1 3
[3,] 2 5 --->>>> [3,] 1 4
[4,] 3 4 [4,] 2 3
[5,] 3 5 [5,] 2 4
[6,] 4 5 [6,] 3 4
The matrix on the right is precisely all of the combinations of 4 choose 2. Continuing on, we see that the "second" group (i.e. rows 7 through 9 of the original matrix) also looks to have order:
[,1] [,2] [,1] [,2]
[1,] 3 4 [1,] 1 2
[2,] 3 5 subtract 2 [2,] 1 3
[3,] 4 5 --->>>> [3,] 2 3
This is simply 3 choose 2. We are starting to see a pattern unfold. Namely, that all combinations of smaller n and r are contained in our parent combinations. This pattern continues as we move to the right. All that is left is to keep up with which combination we are after.
Below is the above algorithm written out in C++ (N.B. there isn't any data validation):
template <typename T>
double nChooseK(T n, T k) {
// Returns number of k-combinations from n elements.
// Mathematically speaking, we have: n!/(k!*(n-k)!)
if (k == n || k == 0)
return 1;
else if (k > n || n < 0)
return 0;
double nCk;
double temp = 1;
for (int i = 1; i <= k; i++)
temp *= (double) (n - k + i) / i;
nCk = std::round(temp);
return nCk;
}
std::vector<int> nthCombination(int n, int r, double i) {
int j = 0, n1 = n - 1, r1 = r - 1;
double temp, index1 = i, index2 = i;
std::vector<int> res(r);
for (int k = 0; k < r; k++) {
temp = nChooseK(n1, r1);
while (temp <= index1) {
index2 -= nChooseK(n1, r1);
n1--;
j++;
temp += nChooseK(n1, r1);
}
res[k] = j;
n1--;
r1--;
j++;
index1 = index2;
}
return res;
}
Calling it on our example above with 5 choose 3 we obtain:
nthCombination(5, 3, 0) -->> 0 1 2
nthCombination(5, 3, 1) -->> 0 1 3
nthCombination(5, 3, 2) -->> 0 1 4
nthCombination(5, 3, 3) -->> 0 2 3
nthCombination(5, 3, 4) -->> 0 2 4
nthCombination(5, 3, 5) -->> 0 3 4
nthCombination(5, 3, 6) -->> 1 2 3
nthCombination(5, 3, 7) -->> 1 2 4
nthCombination(5, 3, 8) -->> 1 3 4
nthCombination(5, 3, 9) -->> 2 3 4
This approach is very efficient as well. Below, we get the billionth combination of 40 choose 20 (which generates more than 100 billion combinations) instantly:
// N.B. base zero so we need to subtract 1
nthCombination(40, 20, 1000000000 - 1) -->>
0 1 2 3 4 5 8 9 14 16 18 20 22 23 31 33 34 35 38 39
Edit
As the OP points out in the comments, they gave an example with repeats. The solution is very similar and it breaks down to counting. We first need a counting function similar to nChooseK but that considers repeats. The function below does just that:
double combsWithReps(int n, int r) {
// For combinations where repetition is allowed, this
// function returns the number of combinations for
// a given n and r. The resulting vector, "triangleVec"
// resembles triangle numbers. In fact, this vector
// is obtained in a very similar method as generating
// triangle numbers, albeit in a repeating fashion.
if (r == 0)
return 1;
int i, k;
std::vector<double> triangleVec(n);
std::vector<double> temp(n);
for (i = 0; i < n; i++)
triangleVec[i] = i+1;
for (i = 1; i < r; i++) {
for (k = 1; k <= n; k++)
temp[k-1] = std::accumulate(triangleVec.begin(), triangleVec.begin() + k, 0.0);
triangleVec = temp;
}
return triangleVec[n-1];
}
And here is the function that generates the ith combination with repeats.
std::vector<int> nthCombWithRep(int n, int r, double i) {
int j = 0, n1 = n, r1 = r - 1;
double temp, index1 = i, index2 = i;
std::vector<int> res(r);
for (int k = 0; k < r; k++) {
temp = combsWithReps(n1, r1);
while (temp <= index1) {
index2 -= combsWithReps(n1, r1);
n1--;
j++;
temp += combsWithReps(n1, r1);
}
res[k] = j;
r1--;
index1 = index2;
}
return res;
}
It is very similar to the first function above. You will notice that n1-- and j++ are removed from the end of the function and also that n1 is initialized to n instead of n - 1.
Here is the above example:
nthCombWithRep(40, 20, 1000000000 - 1) -->>
0 0 0 0 0 0 0 0 0 0 0 4 5 6 8 9 12 18 18 31

Selecting neighbours on a circle

Consider we have N points on a circle. To each point an index is assigned i = (1,2,...,N). Now, for a randomly selected point, I want to have a vector including the indices of 5 points, [two left neighbors, the point itself, two right neighbors].
See the figure below.
Some sxamples are as follows:
N = 18;
selectedPointIdx = 4;
sequence = [2 3 4 5 6];
selectedPointIdx = 1
sequence = [17 18 1 2 3]
selectedPointIdx = 17
sequence = [15 16 17 18 1];
The conventional way to code this is considering the exceptions as if-else statements, as I did:
if ii == 1
lseq = [N-1 N ii ii+1 ii+2];
elseif ii == 2
lseq = [N ii-1 ii ii+1 ii+2];
elseif ii == N-1
lseq=[ii-2 ii-1 ii N 1];
elseif ii == N
lseq=[ii-2 ii-1 ii 1 2];
else
lseq=[ii-2 ii-1 ii ii+1 ii+2];
end
where ii is selectedPointIdx.
It is not efficient if I consider for instance 7 points instead of 5. What is a more efficient way?
How about this -
off = -2:2
out = mod((off + selectedPointIdx) + 17,18) + 1
For a window size of 7, edit off to -3:3.
It uses the strategy of subtracting 1 + modding + adding back 1 as also discussed here.
Sample run -
>> off = -2:2;
for selectedPointIdx = 1:18
disp(['For selectedPointIdx =',num2str(selectedPointIdx),' :'])
disp(mod((off + selectedPointIdx) + 17,18) + 1)
end
For selectedPointIdx =1 :
17 18 1 2 3
For selectedPointIdx =2 :
18 1 2 3 4
For selectedPointIdx =3 :
1 2 3 4 5
For selectedPointIdx =4 :
2 3 4 5 6
For selectedPointIdx =5 :
3 4 5 6 7
For selectedPointIdx =6 :
4 5 6 7 8
....
For selectedPointIdx =11 :
9 10 11 12 13
For selectedPointIdx =12 :
10 11 12 13 14
For selectedPointIdx =13 :
11 12 13 14 15
For selectedPointIdx =14 :
12 13 14 15 16
For selectedPointIdx =15 :
13 14 15 16 17
For selectedPointIdx =16 :
14 15 16 17 18
For selectedPointIdx =17 :
15 16 17 18 1
For selectedPointIdx =18 :
16 17 18 1 2
You can use modular arithmetic instead: Let p be the point among N points numbered 1 to N. Say you want m neighbors on each side, you can get them as follows:
(p - m - 1) mod N + 1
...
(p - 4) mod N + 1
(p - 3) mod N + 1
(p - 2) mod N + 1
p
(p + 1) mod N + 1
(p + 2) mod N + 1
(p + 3) mod N + 1
...
(p + m - 1) mod N + 1
Code:
N = 18;
p = 2;
m = 3;
for i = p - m : p + m
nb = mod((i - 1) , N) + 1;
disp(nb);
end
Run code here
I would like you to note that you might not necessarily improve performance by avoiding a if statement. A benchmark might be necessary to figure this out. However, this will only be significant if you are treating tens of thousands of numbers.

How to define contrast coefficient matrix?

I have this data
y x1 x2 pre
1 16 1 1 14
2 15 1 1 13
3 14 1 2 14
4 13 1 2 13
5 12 2 1 12
6 11 2 1 12
7 11 2 2 13
8 13 2 2 13
9 10 3 1 10
10 11 3 1 11
11 11 3 2 11
12 9 3 2 10
And I fitted the following model
lm(y ~ x1 + x2 + x1*x2)
My design matrix is
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1 14 1 0 1 1 0
[2,] 1 13 1 0 1 1 0
[3,] 1 14 1 0 0 0 0
[4,] 1 13 1 0 0 0 0
[5,] 1 12 0 1 1 0 1
[6,] 1 12 0 1 1 0 1
[7,] 1 13 0 1 0 0 0
[8,] 1 13 0 1 0 0 0
[9,] 1 10 0 0 1 0 0
[10,] 1 11 0 0 1 0 0
[11,] 1 11 0 0 0 0 0
[12,] 1 10 0 0 0 0 0
I'm trying to use this design to reproduce the following table:
Source DF Squares Mean Square F Value Pr > F
Model 6 44.79166667 7.46527778 12.98 0.0064
Error 5 2.87500000 0.57500000
Corrected Total 11 47.66666667
Source DF Type III SS Mean Square F Value Pr > F
pre 1 3.12500000 3.12500000 5.43 0.0671
x1 2 4.58064516 2.29032258 3.98 0.0923
x2 1 3.01785714 3.01785714 5.25 0.0706
x1*x2 2 1.25000000 0.62500000 1.09 0.4055
The first part is fine
XtX <- t(x) %*% x
XtXinv <- solve(XtX)
betahat <- XtXinv %*% t(x) %*% y
H <- x %*% XtXinv %*% t(x)
IH <- (diag(1,12) - H)
yhat <- H %*% y
e <- IH %*% y
ybar <- mean(y)
MSS <- t(betahat) %*% t(x) %*% y - length(y)*(ybar^2)
ESS <- t(e) %*% e
TSS <- MSS + ESS
dfM <- sum(diag(H)) - 1
dfE <- sum(diag(IH))
dfT <- dfM + dfE
MSM <- MSS/dfM
MSE <- ESS/dfE
Ftest <- MSM / MSE
pr <- 1 - pf(Ftest, dfM, dfE)
The contrast coefficient matrix for 'pre' seems correct.
L <- matrix(c(0,1,0,0,0,0,0), 1, 7, byrow=T)
Lb <- L %*% betahat
LXtXinvLt <- round(L %*% XtXinv %*% t(L), digits=4)
SSpre <- t(Lb) %*% solve(LXtXinvLt) %*% (Lb)
MSpre <- SSpre / 1
Fpre <- MSpre / MSE
PRpre <- 1 - pf(Fpre, 1, 12-7)
But I can't understand how to define the contrast coefficient matrix for x1, x2, and x1*x2. What's the problem with the rest of my code? Below an example for how I think I should calculate for x1
L <- matrix(c(0,0,1,1,0,0,0), 1, 7, byrow=T)
Lb <- L %*% betahat
LXtXinvLt <- round(L %*% XtXinv %*% t(L), digits=4)
SSX1 <- t(Lb) %*% solve(LXtXinvLt) %*% (Lb)
MSX1 <- SSX1 / 1
FX1 <- MSX1 / MSE
PRX1 <- 1 - pf(FX1, 1, 12-7)
Thanks!

Resources