Package writing.
- To: mathgroup at christensen.cybernetics.net
- 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
Hi, 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 ananke.mit.edu 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: *) BeginPackage["StepRegress`", "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 *) Go=True; 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, OutputControl->NoPrint, OutputList->{EstimatedVariance, 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 ]] ]]; ] ,{i,n}]; finalregress = DesignedRegress[ Column[datamatrix, varlist], rdata, BasisNames -> varlist, OutputList ->{PredictedResponse} ]; Flatten[{Coeffs->Flatten[beta], VarList->varlist,RSquaredList->RRlist,finalregress}] ]; (* end of module *) Protect[ StepRegress ] EndPackage[];