Homework #3 

1. YIELDFIT Program handed out in class

2.  Linear-plateau program

/* A. Results from Soil Test P, Bray P-1 (bkp1) and
Mehlich III (miii) and percent maximum yield (pmax) */

data one;
input farmer miii bkp1 yield;
cards;
1 5 11.5 1100
2 14 16 1400
3 18 19 2006
4 30 25 3007
5 32 28 3600
6 31 23 3509
data one; set one;
bkp12 = bkp1 * bkp1;
miii2 = miii * miii;
sqbk = sqrt(bkp1);
sqmi = sqrt(miii);
pmax = yield/3600;


/* ---------------------------------------------------
1. Quadratic and Square Root Equations of PMAX versus BKP1 and MIII
---------------------------------------------------- */

proc glm data = one;
  model pmax = bkp1 bkp12/p cli;
  output out = bk p = ppmax l95=l95 u95=u95;
  symbol1 i = spline;
proc plot data = bk;
  plot ppmax*bkp1='p' pmax*bkp1 = 'o' l95*bkp1='l' u95*bkp1='u'/overlay
  vaxis = 0 to 1.5 by 0.1 haxis = 5 to 35 by 5;
proc glm data = one;
  model pmax = bkp1 sqbk/p cli;
  output out = bk2 p = ppmax l95=l95 u95=u95;
  symbol1 i = spline;
proc plot data = bk2;
  plot ppmax*bkp1='p' pmax*bkp1 = 'o' l95*bkp1='l' u95*bkp1='u'/overlay
  vaxis = 0 to 1.5 by 0.1 haxis = 5 to 35 by 5;
proc glm data = one;
  model pmax = miii miii2/p cli;
  output out = mi p = ppmax l95=l95 u95=u95;
  symbol1 i = spline;
proc plot data = mi;
  plot ppmax*miii='p' pmax*miii = 'o' l95*miii='l' u95*miii='u'/overlay
  vaxis = 0 to 1.5 by 0.1 haxis = 5 to 35 by 5;
proc glm data = one;
  model pmax = miii sqmi/p cli;
  output out = mi2 p = ppmax l95=l95 u95=u95;
  symbol1 i = spline;
proc plot data = mi2;
  plot ppmax*miii='p' pmax*miii = 'o' l95*miii='l' u95*miii='u'/overlay
  vaxis = 0 to 1.5 by 0.1 haxis = 5 to 35 by 5;

/* ----------------------------------------------------
2. Linear Plateau of percent maximum yield (PMAX)
and Mehlich III soil test P (MIII)
------------------------------------------------------- */

PROC NLIN DATA = one BEST = 2;
PARMS B0=____ to __ by __ B1=_____ to ___ by ___ PJOINT=___ to ___ by ___;
IF miii<PJOINT THEN DO;
  MODEL pmax = B0 + B1*miii;
  DER.B0=1;
  DER.B1=miii;
  DER.PJOINT=0;
END;
ELSE DO;
  MODEL pmax=B0+B1*PJOINT;
DER.B0=1;
DER.B1=PJOINT;
DER.PJOINT=B1;
END;
  FILE PRINT;
  IF _obs_ =1 AND _MODEL_ =0 THEN DO;
  PLATEAU = B0 + B1*PJOINT;
PUT PLATEAU=;
END;
PLATEAU=B0+B1*PJOINT;
ID PLATEAU;
OUTPUT OUT = NEW P = ppmax PARMS=B0 B1 PJOINT SSE=SSE;
RUN;
PROC PLOT;
  PLOT pmax*miii='+' ppmax*miii='*'/OVERLAY
  vaxis = 0 to 1.5 by 0.1 haxis = 0 to 40 by 5;
RUN;
PROC MEANS NOPRINT;
  VAR pmax SSE B0 B1 PJOINT PLATEAU;
  OUTPUT OUT = NEW2 N = TDF
  MEAN = pmax SSE B0 B1 PJOINT PLATEAU
  CSS=CSST;
DATA NEW3; SET NEW2;
  INTERCPT=B0; SLOPE=B1; JOINT=pJOINT;
  RSQ=(CSST-SSE)/CSST;
  EDF=TDF-3;
  SSR=CSST-SSE;
  MSR=SSR/2;
  MSE=SSE/EDF;
  F=MSR/MSE;
  PROBF=1-(PROBF(F,2,EDF));
KEEP INTERCPT SLOPE JOINT PLATEAU RSQ F PROBF;
PROC PRINT;
RUN;

/* ---------------------------------------------------
B. Results from P Rate Experiments with the Same Farmers
------------------------------------------------------ */

data two;
input farmer Prate yield;
cards;
1 0 1015
1 20 1650
1 40 2412
1 80 2815
1 120 3310
1 160 3499
1 200 3501
2 0 1400
2 20 2350
2 40 2950
2 80 3150
2 120 3450
2 160 3490
2 200 3470
3 0 2416
3 20 2760
3 40 3240
3 80 3490
3 120 3504
3 160 3506
3 200 3501
4 0 3080
4 20 3260
4 40 3450
4 80 3498
4 120 3502
4 160 3522
4 200 3518
data two; set two;
prate2 = prate*prate;
pmax = yield/3522;
prsq = sqrt(prate);


/* ---------------------------------------------------
1. Quadratic & SQRT Equations of Yield versus PRATE
------------------------------------------------------ */

data three; set two;
proc sort; by farmer;
proc glm; by farmer;
model yield = prate prate2/p cli;
output out = pr p = ppmax l95=l95 u95=u95;
symbol1 i = spline;
symbol2 i = spline;
symbol3 i = spline;
symbol4 i = spline;
symbol5 i = spline;
symbol6 i = spline;
proc plot data = pr; by farmer;
plot ppmax*prate='p' pmax*prate = 'o' l95*prate='l' u95*prate='u'/overlay
vaxis = 0 to 4200 by 400 haxis = 0 to 200 by 20;
run;
data threex; set two;
proc sort; by farmer;
proc glm; by farmer;
model yield = prate prsq/p cli;
output out = prsq p = ppmax l95=l95 u95=u95;
symbol1 i = spline;
symbol2 i = spline;
symbol3 i = spline;
symbol4 i = spline;
symbol5 i = spline;
symbol6 i = spline;
proc plot data = prsq; by farmer;
plot ppmax*prate='p' pmax*prate = 'o' l95*prate='l' u95*prate='u'/overlay
vaxis = 0 to 4200 by 400 haxis = 0 to 200 by 20;
run;

/* ---------------------------------------------------
2. Linear Plateau of percent maximum yield (PMAX)
and PRATE (by farmer)
------------------------------------------------------ */

data five; set two;
if farmer > 1 then delete;

/* ---------------------------------------------------
2a. (Farmer 1)
------------------------------------------------------ */

PROC NLIN DATA = five BEST = 2;
PARMS B0=____ to __ by __ B1=_____ to ___ by ___ PJOINT=___ to ___ by ___;
IF Prate<PJOINT THEN DO;
MODEL pmax = B0 + B1*prate;
DER.B0=1;
DER.B1=Prate;
DER.PJOINT=0;
END;
ELSE DO;
MODEL pmax=B0+B1*PJOINT;
DER.B0=1;
DER.B1=PJOINT;
DER.PJOINT=B1;
END;
FILE PRINT;
IF _obs_ =1 AND _MODEL_ =0 THEN DO;
PLATEAU = B0 + B1*PJOINT;
PUT PLATEAU=;
END;
PLATEAU=B0+B1*PJOINT;
ID PLATEAU;
OUTPUT OUT = NEW P = ppmax PARMS=B0 B1 PJOINT SSE=SSE;
RUN;
PROC PLOT;
PLOT pmax*prate='+' ppmax*prate='*'/OVERLAY
vaxis = 0 to 1.5 by 0.1 haxis = 0 to 210 by 20;
RUN;
PROC MEANS NOPRINT;
VAR pmax SSE B0 B1 PJOINT PLATEAU;
OUTPUT OUT = NEW2 N = TDF
MEAN = pmax SSE B0 B1 PJOINT PLATEAU
CSS=CSST;
DATA NEW3; SET NEW2;
INTERCPT=B0; SLOPE=B1; JOINT=pJOINT;
RSQ=(CSST-SSE)/CSST;
EDF=TDF-3;
SSR=CSST-SSE;
MSR=SSR/2;
MSE=SSE/EDF;
F=MSR/MSE;
PROBF=1-(PROBF(F,2,EDF));
KEEP INTERCPT SLOPE JOINT PLATEAU RSQ F PROBF;
PROC PRINT;
RUN;

data six; set two;
if farmer = 1 then delete;
if farmer > 2 then delete;

/* ---------------------------------------------------
2b. (Farmer 2)
------------------------------------------------------ */

PROC NLIN DATA = six BEST = 2;
PARMS B0=____ to __ by __ B1=_____ to ___ by ___ PJOINT=___ to ___ by ___;
IF Prate<PJOINT THEN DO;
MODEL pmax = B0 + B1*prate;
DER.B0=1;
DER.B1=Prate;
DER.PJOINT=0;
END;
ELSE DO;
MODEL pmax=B0+B1*PJOINT;
DER.B0=1;
DER.B1=PJOINT;
DER.PJOINT=B1;
END;
FILE PRINT;
IF _obs_ =1 AND _MODEL_ =0 THEN DO;
PLATEAU = B0 + B1*PJOINT;
PUT PLATEAU=;
END;
PLATEAU=B0+B1*PJOINT;
ID PLATEAU;
OUTPUT OUT = NEW P = ppmax PARMS=B0 B1 PJOINT SSE=SSE;
RUN;
PROC PLOT;
PLOT pmax*prate='+' ppmax*prate='*'/OVERLAY
vaxis = 0 to 1.5 by 0.1 haxis = 0 to 210 by 20;
RUN;
PROC MEANS NOPRINT;
VAR pmax SSE B0 B1 PJOINT PLATEAU;
OUTPUT OUT = NEW2 N = TDF
MEAN = pmax SSE B0 B1 PJOINT PLATEAU
CSS=CSST;
DATA NEW3; SET NEW2;
INTERCPT=B0; SLOPE=B1; JOINT=pJOINT;
RSQ=(CSST-SSE)/CSST;
EDF=TDF-3;
SSR=CSST-SSE;
MSR=SSR/2;
MSE=SSE/EDF;
F=MSR/MSE;
PROBF=1-(PROBF(F,2,EDF));
KEEP INTERCPT SLOPE JOINT PLATEAU RSQ F PROBF;
PROC PRINT;
RUN;

data seven; set two;
if farmer = 1 then delete;
if farmer = 2 then delete;
if farmer > 3 then delete;

/* ---------------------------------------------------
2c. (Farmer 3)
------------------------------------------------------ */

PROC NLIN DATA = seven BEST = 2;
PARMS B0=____ to __ by __ B1=_____ to ___ by ___ PJOINT=___ to ___ by ___;
IF Prate<PJOINT THEN DO;
MODEL pmax = B0 + B1*prate;
DER.B0=1;
DER.B1=Prate;
DER.PJOINT=0;
END;
ELSE DO;
MODEL pmax=B0+B1*PJOINT;
DER.B0=1;
DER.B1=PJOINT;
DER.PJOINT=B1;
END;
FILE PRINT;
IF _obs_ =1 AND _MODEL_ =0 THEN DO;
PLATEAU = B0 + B1*PJOINT;
PUT PLATEAU=;
END;
PLATEAU=B0+B1*PJOINT;
ID PLATEAU;
OUTPUT OUT = NEW P = ppmax PARMS=B0 B1 PJOINT SSE=SSE;
RUN;
PROC PLOT;
PLOT pmax*prate='+' ppmax*prate='*'/OVERLAY
vaxis = 0 to 1.5 by 0.1 haxis = 0 to 210 by 20;
RUN;
PROC MEANS NOPRINT;
VAR pmax SSE B0 B1 PJOINT PLATEAU;
OUTPUT OUT = NEW2 N = TDF
MEAN = pmax SSE B0 B1 PJOINT PLATEAU
CSS=CSST;
DATA NEW3; SET NEW2;
INTERCPT=B0; SLOPE=B1; JOINT=pJOINT;
RSQ=(CSST-SSE)/CSST;
EDF=TDF-3;
SSR=CSST-SSE;
MSR=SSR/2;
MSE=SSE/EDF;
F=MSR/MSE;
PROBF=1-(PROBF(F,2,EDF));
KEEP INTERCPT SLOPE JOINT PLATEAU RSQ F PROBF;
PROC PRINT;
RUN;

data eight; set two;
if farmer = 1 then delete;
if farmer = 2 then delete;
if farmer = 3 then delete;
if farmer > 4 then delete;

/* ---------------------------------------------------
2d. (Farmer 4)
------------------------------------------------------ */

PROC NLIN DATA = eight BEST = 2;
PARMS B0=____ to __ by __ B1=_____ to ___ by ___ PJOINT=___ to ___ by ___;
IF Prate<PJOINT THEN DO;
MODEL pmax = B0 + B1*prate;
DER.B0=1;
DER.B1=Prate;
DER.PJOINT=0;
END;
ELSE DO;
MODEL pmax=B0+B1*PJOINT;
DER.B0=1;
DER.B1=PJOINT;
DER.PJOINT=B1;
END;
FILE PRINT;
IF _obs_ =1 AND _MODEL_ =0 THEN DO;
PLATEAU = B0 + B1*PJOINT;
PUT PLATEAU=;
END;
PLATEAU=B0+B1*PJOINT;
ID PLATEAU;
OUTPUT OUT = NEW P = ppmax PARMS=B0 B1 PJOINT SSE=SSE;
RUN;
PROC PLOT;
PLOT pmax*prate='+' ppmax*prate='*'/OVERLAY
vaxis = 0 to 1.5 by 0.1 haxis = 0 to 210 by 20;
RUN;
PROC MEANS NOPRINT;
VAR pmax SSE B0 B1 PJOINT PLATEAU;
OUTPUT OUT = NEW2 N = TDF
MEAN = pmax SSE B0 B1 PJOINT PLATEAU
CSS=CSST;
DATA NEW3; SET NEW2;
INTERCPT=B0; SLOPE=B1; JOINT=pJOINT;
RSQ=(CSST-SSE)/CSST;
EDF=TDF-3;
SSR=CSST-SSE;
MSR=SSR/2;
MSE=SSE/EDF;
F=MSR/MSE;
PROBF=1-(PROBF(F,2,EDF));
KEEP INTERCPT SLOPE JOINT PLATEAU RSQ F PROBF;
PROC PRINT;
RUN;

/*
PRATE = phosphorus rate, kg/ha
PMAX = relative yield, %
B0 = intercept
B1 = slope
PJOINT = PRATE at which the two lines join
*/