MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Package writing.

  • To: mathgroup at
  • Subject: [mg1079] Package writing.
  • From: forest at ananke.MIT.EDU (Chris E. Forest)
  • Date: Fri, 12 May 1995 18:31:06 -0400
  • Organization: Massachvsetts Institvte of Technology


I am having some difficulty writing a package and thought someone could  
help out.  I can define a function in a session and use it, but it doesn't  
work by loading the package as I'd hoped.  

My problem is, I think, that within the package's context I am trying
to use a function from another context but it can't find it. 
Do I have to put the full name to use it? or is there an appropriate way  
for declaring a package within another package?   
BeginPackage["StepRegress`", "Statistics`Master`" ] didn't seem to solve  
it. Even after reading The Book, I'm a little confused on using this.

Needed Information:
The new function is StepRegress[ datamatrix_, rdata_ ]
It can't find DesignedRegress or Column which are defined in the 
Statistics` packages.  Dimensions[datamatrix]~={100,30}

If someone has hints on speeding this up as well, it'd be greatly  
appreciated. Also, is defining all the variables in a Module necessary  
within a package? It seems to hog space.  Please note that the package is  
not ready for public dispersal.

E-mail is preferable. Thanks for any help. 

Chris E. Forest
forest at

Here is StepRegress.m 

(* :Title:  Package StepRegress *)
(* :Context: *)
(* :Author:  Chris E. Forest *)
(* :Summary:
	This package provides a function for doing forward
	stepwise regression.  Later editions will include
	backwards, and mixed stepwise regression routines.
(* :Package Version:  1.0  *)
(* :Mathematica Version: 2.0 *)
(* :Copyright: None *)
(* :History:
	V. 1.0 April 1995, by Chris E. Forest 
(* :Keywords:
	stepwise regression, regression, statistics
(* :Limitations: 
	Many Mma features are not implemented yet.  Options are
	not available for the final regression.  Error checking
	is not done yet.  Main drawback is the slowness which is because 
	of the excessive use of Do loops. An options list will
	help the user choose the appropriate output list.
(* :Discussion:  *)

		"Statistics`Master`" ]

StepRegress::usage = "StepRegress[ designmatrix_, rdata_ ] will 
	perform a forward stepwise regression with a p-value cutoff
	of 0.15.  New variables are added provided they are significant
	at the 0.15 level and the overall variance is reduced."
Unprotect[ StepRegress ]
StepRegress[ datamatrix_, rdata_ ]:=
Module[ {m, n, i, j, varlist, RRlist, testout, 
	var, bestvar, newvar, RR, oldRR, flag, Go,  
	pvalue, newp, best, finalregress, beta}, 

(* Need to check matrix, rdata, sizes. On error, print message *)
m = Length[datamatrix]; (* Rows= Num. Datapoints *)
n = Length[Transpose[datamatrix]];(*Columns= Num. Variables*)
varlist = {}; RRlist = {}; bestvar = -1; 
(* bestvar = best estimated variance of regression  *)
(* varlist = list of added variables *)

While[ Go, (
	newvar = -1; (* reset after each addition *)
(* check for adding new variable *)
Do[ If[ !MemberQ[ varlist, i ],
(	testout = DesignedRegress[ 
		Column[ datamatrix, Flatten[{varlist,i}] ], rdata, 
			ParameterTable, AdjustedRSquared} ];
	var = EstimatedVariance /. testout;
	pvalue = Last[ Last[ First[
		ParameterTable /. testout ] ] ]; (* get p-value *)
	RR = AdjustedRSquared /. testout;
	If[ (var < bestvar||newvar < 0),
		(bestvar = var; newvar = 1; flag = i;
		oldRR = RR; newp = pvalue;) ];
)];(* End If *)
,{i,1,n}];  (* End Do *)
(* need to check p-value of coefficient *)
If[ newp < 0.15 , 
	(AppendTo[varlist, flag]; AppendTo[RRlist, oldRR ];),
	(Go=False;) ];
If[ Length[varlist] == n, Go = False; ];
) ]; (* End While *)

(* get best fit coefficients *)
best = BestFitCoefficients /. DesignedRegress[ 
	Column[datamatrix, varlist], rdata, 
	OutputControl-> NoPrint,
	OutputList-> {BestFitCoefficients} ] ;
beta = Table[ 0,{n} ];
 Table[ If[ MemberQ[ varlist, i ] , 
	beta[[i]] = best[[Flatten[Position[ varlist, i ]] ]]; ]
finalregress = DesignedRegress[ 
	Column[datamatrix, varlist], rdata, BasisNames -> varlist,
	OutputList ->{PredictedResponse} ];
]; (* end of module *)

Protect[ StepRegress ]


  • Prev by Date: Re: The Case of the Mystery Option
  • Next by Date: speed benchmarks?
  • Previous by thread: Re:Challenge! ....RESULTS..., (Corrected)
  • Next by thread: speed benchmarks?