grover11
05-04-2009, 01:10 PM
Hi,
I am using JMSL 5.0.1 on WinXP with SP2 and JRE 1.6.0_06. I am using the NonlinearRegression class to generate the coefficients a and b in the equation Y = a * e^(b * x). I generated the initial parameters values for these coefficients with some sample data, and provided them to the setGuess(), where a = 18.755497533160803 and b = 0.00022960312118070167.
I used the initial parameter values above with nls() in R, which by default uses a Gauss-Newton algorithm, and it generated the coefficients for the exponential equation as a = 850.7 and b = 0.0005322.
In addition, I used MS Excel to create a scatterplot with an exponential trendline. The exponential equation has a = 854.06 and c = 0.0005. I?m not sure what algorithm Excel is using to generate these coefficients.
When I generate the coefficients using JMSL, solve() returns a= 71.84244133504096 and b = 0.0020465827799000125. I am puzzled as to why the coefficients for a and b generated by JMSL differ from those generated by R and Excel. Would someone please let me know what is wrong with my implementation below?
My sample data is attached; it is a CSV file with the extension txt.
Thank you.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Vector;
import com.imsl.stat.NonlinearRegression;
public class ExponentialCoefficients {
private File csvFile;
private double[] xinput;
private double[] yinput;
private int nobs;
private double estimated_a;
private double estimated_b;
//calculate coefficients a and b in predicted y = a * e^(b * x)
public static void main(String[] args){
try{
ExponentialCoefficients exp = new ExponentialCoefficients();
exp.calculateCoefficients();
}
catch(Exception e){
e.printStackTrace();
}
}
public ExponentialCoefficients() throws Exception{
csvFile = new File("<File path>\\xy_values.txt");
parseCSVLines(readTextFileLines(csvFile));
nobs = xinput.length;
estimated_a = 18.755497533160803;
estimated_b = 0.00022960312118070167;
}
public void calculateCoefficients() throws Exception{
int nparam = 2;
double theta[] = new double[2];
theta[0] = estimated_a;
theta[1] = estimated_b;
NonlinearRegression regression = new NonlinearRegression(nparam);
regression.setGuess(theta);
double coef[] = regression.solve(f);
System.out.println("The computed regression coefficients are {" +
coef[0] + ", " + coef[1] + "}");
}
NonlinearRegression.Function f = new NonlinearRegression.Function() {
public boolean f(double theta[], int iobs, double frq[],
double wt[], double e[]){
boolean iend;
if(iobs < nobs){
wt[0] = 1.0;
frq[0] = 1.0;
iend = true;
//System.out.println("theta[0]: "+theta[0] + "\t theta[1]: " + theta[1]);
double predictedY = theta[0] * Math.exp(theta[1] * xinput[iobs]);
e[0] = yinput[iobs] - predictedY;
} else {
iend = false;
}
return iend;
}
};
//Read in lines from Text file and load lines (rows) into vector.
public Vector<String> readTextFileLines(File textFile)throws Exception {
Vector<String> csvLines = new Vector<String>();
//read file
BufferedReader reader = new BufferedReader(new FileReader(textFile));
//read in each line
String line = null;
while( (line = reader.readLine()) != null ){
csvLines.add(line);
}
reader.close();
return csvLines;
}
//Parse lines into individual values and store them.
public void parseCSVLines(Vector<String> csvLines)throws Exception{
// remove header row
csvLines.remove(0);
//initialize double[][]
xinput = new double[csvLines.size()];
yinput = new double[csvLines.size()];
//populate double[][] with values
for(int index = 0; index < csvLines.size(); index++){
String row = csvLines.get(index);
String[] xyvalues = row.split(",");
xinput[index] = Double.parseDouble(xyvalues[0]);
yinput[index] = Double.parseDouble(xyvalues[1]);
}
}
}//end
I am using JMSL 5.0.1 on WinXP with SP2 and JRE 1.6.0_06. I am using the NonlinearRegression class to generate the coefficients a and b in the equation Y = a * e^(b * x). I generated the initial parameters values for these coefficients with some sample data, and provided them to the setGuess(), where a = 18.755497533160803 and b = 0.00022960312118070167.
I used the initial parameter values above with nls() in R, which by default uses a Gauss-Newton algorithm, and it generated the coefficients for the exponential equation as a = 850.7 and b = 0.0005322.
In addition, I used MS Excel to create a scatterplot with an exponential trendline. The exponential equation has a = 854.06 and c = 0.0005. I?m not sure what algorithm Excel is using to generate these coefficients.
When I generate the coefficients using JMSL, solve() returns a= 71.84244133504096 and b = 0.0020465827799000125. I am puzzled as to why the coefficients for a and b generated by JMSL differ from those generated by R and Excel. Would someone please let me know what is wrong with my implementation below?
My sample data is attached; it is a CSV file with the extension txt.
Thank you.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Vector;
import com.imsl.stat.NonlinearRegression;
public class ExponentialCoefficients {
private File csvFile;
private double[] xinput;
private double[] yinput;
private int nobs;
private double estimated_a;
private double estimated_b;
//calculate coefficients a and b in predicted y = a * e^(b * x)
public static void main(String[] args){
try{
ExponentialCoefficients exp = new ExponentialCoefficients();
exp.calculateCoefficients();
}
catch(Exception e){
e.printStackTrace();
}
}
public ExponentialCoefficients() throws Exception{
csvFile = new File("<File path>\\xy_values.txt");
parseCSVLines(readTextFileLines(csvFile));
nobs = xinput.length;
estimated_a = 18.755497533160803;
estimated_b = 0.00022960312118070167;
}
public void calculateCoefficients() throws Exception{
int nparam = 2;
double theta[] = new double[2];
theta[0] = estimated_a;
theta[1] = estimated_b;
NonlinearRegression regression = new NonlinearRegression(nparam);
regression.setGuess(theta);
double coef[] = regression.solve(f);
System.out.println("The computed regression coefficients are {" +
coef[0] + ", " + coef[1] + "}");
}
NonlinearRegression.Function f = new NonlinearRegression.Function() {
public boolean f(double theta[], int iobs, double frq[],
double wt[], double e[]){
boolean iend;
if(iobs < nobs){
wt[0] = 1.0;
frq[0] = 1.0;
iend = true;
//System.out.println("theta[0]: "+theta[0] + "\t theta[1]: " + theta[1]);
double predictedY = theta[0] * Math.exp(theta[1] * xinput[iobs]);
e[0] = yinput[iobs] - predictedY;
} else {
iend = false;
}
return iend;
}
};
//Read in lines from Text file and load lines (rows) into vector.
public Vector<String> readTextFileLines(File textFile)throws Exception {
Vector<String> csvLines = new Vector<String>();
//read file
BufferedReader reader = new BufferedReader(new FileReader(textFile));
//read in each line
String line = null;
while( (line = reader.readLine()) != null ){
csvLines.add(line);
}
reader.close();
return csvLines;
}
//Parse lines into individual values and store them.
public void parseCSVLines(Vector<String> csvLines)throws Exception{
// remove header row
csvLines.remove(0);
//initialize double[][]
xinput = new double[csvLines.size()];
yinput = new double[csvLines.size()];
//populate double[][] with values
for(int index = 0; index < csvLines.size(); index++){
String row = csvLines.get(index);
String[] xyvalues = row.split(",");
xinput[index] = Double.parseDouble(xyvalues[0]);
yinput[index] = Double.parseDouble(xyvalues[1]);
}
}
}//end