<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Fit Spline for multiple columns and saving predicted values in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Fit-Spline-for-multiple-columns-and-saving-predicted-values/m-p/801578#M97729</link>
    <description>&lt;P&gt;This might give some ideas&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Semiconductor Capability.jmp");

xcol = "NPN1";
ycols = {"PNP1", "NPN2"};

fits = {};
For Each({ycol}, ycols,
	Insert Into(fits,
		dt &amp;lt;&amp;lt; Fit Y by X(
			Y(Eval(ycol)),
			X(Eval(xcol)),
			Fit Spline(1000)	
		);
	);
);

close_expr = Expr(
	For Each({fit}, fits,
		fit &amp;lt;&amp;lt; close window;
	);
	nw &amp;lt;&amp;lt; Close Window;
);

nw = New Window("Save Predicted Values?", 
	Text Box("Are you satisfied with the fits? Click Yes to save predicted values, No to stop."), 
	Button Box("Yes", 
		For Each({fit}, fits,
			fit &amp;lt;&amp;lt; (Curve["Smoothing Spline Fit, lambda=1000"] &amp;lt;&amp;lt; Save Predicteds);
		);
		wait(0);
		close_expr;
		Show("Predicted values saved successfully.");
	), 
	Button Box("No", 
		Show("Predicted values were not saved.");
		close_expr;
	)
);

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would also collect all the Fit Y by X to single window so it is easier to check them instead of opening multiple separate windows&lt;/P&gt;</description>
    <pubDate>Tue, 24 Sep 2024 14:32:25 GMT</pubDate>
    <dc:creator>jthi</dc:creator>
    <dc:date>2024-09-24T14:32:25Z</dc:date>
    <item>
      <title>Fit Spline for multiple columns and saving predicted values</title>
      <link>https://community.jmp.com/t5/Discussions/Fit-Spline-for-multiple-columns-and-saving-predicted-values/m-p/801459#M97710</link>
      <description>&lt;P&gt;Hi all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a lot of different columns from an FTIR spectra experiment, and in order to correct the baseline, I need to perform a Fit Spline on each column (one column = one spectra). I would like to do this automatically using JSL, so I don't have to save hundreds of "Predicted values" by hand.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to create a code but somehow it doesn't want to save the values or put 0 in each columns.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could anyone help for that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Below is my code:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = Current Data Table();

// Define the columns for the fit (assuming multiple Y columns and one X column)
x_col = Column("Wavenumber"); // Replace with the name of your X column

y_cols = {};
For(i = 1, i &amp;lt;= N Col(dt), i++, 
	col_name = Column(i) &amp;lt;&amp;lt; Get Name;
    // Check if the column name contains "Absorbance at t="
	If(Contains(col_name, "Absorbance at t="), 
		Insert Into(y_cols, col_name);  // Add to the list
	);
);

// Create a list to store fit reports
fit_reports = {};

// Perform Fit Y by X for each Y column separately and display the fit
For(i = 1, i &amp;lt;= N Items(y_cols), i++, 
	y_col = y_cols[i];
	fit_report = dt &amp;lt;&amp;lt; Fit Y by X(
		Y(Column(y_col)), 
		X(x_col), 
		Fit Spline(1000) // Adjust the lambda (smoothing parameter) as needed
	);
	Insert Into(fit_reports, fit_report);
);

Show(
	"All fits have been displayed. Please review them and adjust the lambda value in the script if needed."
);

// Wait for user to review fits
Wait(0);

// Use a Yes/No dialog to prompt the user to continue or stop
result = New Window("Save Predicted Values?", 
	Text Box("Are you satisfied with the fits? Click Yes to save predicted values, No to stop."), 
	Button Box("Yes", 
        // Save predicted values as columns in the dataset
		For(i = 1, i &amp;lt;= N Items(y_cols), i++, 
			y_col = y_cols[i];
			fit_report = fit_reports[i];
			predicted_col_name = "Spline Predictor for " || y_col;
			dt &amp;lt;&amp;lt; New Column(predicted_col_name, Numeric);
			Column(predicted_col_name) &amp;lt;&amp;lt; Set Values(fit_report["Prediction Formula"]);
		);
		Show("Predicted values saved successfully.");
	), 
	Button Box("No", 
		Show("Predicted values were not saved.");
	)
);

// Close all fit reports
For(i = 1, i &amp;lt;= N Items(fit_reports), i++, 
	Close(fit_reports[i], No Save);
);
 

Close(result); // Close the dialog window
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit 2024-09-24 jthi: added jsl formatting&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 06:29:44 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Fit-Spline-for-multiple-columns-and-saving-predicted-values/m-p/801459#M97710</guid>
      <dc:creator>LGaspard_98</dc:creator>
      <dc:date>2024-09-24T06:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: Fit Spline for multiple columns and saving predicted values</title>
      <link>https://community.jmp.com/t5/Discussions/Fit-Spline-for-multiple-columns-and-saving-predicted-values/m-p/801578#M97729</link>
      <description>&lt;P&gt;This might give some ideas&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = Open("$SAMPLE_DATA/Semiconductor Capability.jmp");

xcol = "NPN1";
ycols = {"PNP1", "NPN2"};

fits = {};
For Each({ycol}, ycols,
	Insert Into(fits,
		dt &amp;lt;&amp;lt; Fit Y by X(
			Y(Eval(ycol)),
			X(Eval(xcol)),
			Fit Spline(1000)	
		);
	);
);

close_expr = Expr(
	For Each({fit}, fits,
		fit &amp;lt;&amp;lt; close window;
	);
	nw &amp;lt;&amp;lt; Close Window;
);

nw = New Window("Save Predicted Values?", 
	Text Box("Are you satisfied with the fits? Click Yes to save predicted values, No to stop."), 
	Button Box("Yes", 
		For Each({fit}, fits,
			fit &amp;lt;&amp;lt; (Curve["Smoothing Spline Fit, lambda=1000"] &amp;lt;&amp;lt; Save Predicteds);
		);
		wait(0);
		close_expr;
		Show("Predicted values saved successfully.");
	), 
	Button Box("No", 
		Show("Predicted values were not saved.");
		close_expr;
	)
);

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would also collect all the Fit Y by X to single window so it is easier to check them instead of opening multiple separate windows&lt;/P&gt;</description>
      <pubDate>Tue, 24 Sep 2024 14:32:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Fit-Spline-for-multiple-columns-and-saving-predicted-values/m-p/801578#M97729</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2024-09-24T14:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Fit Spline for multiple columns and saving predicted values</title>
      <link>https://community.jmp.com/t5/Discussions/Fit-Spline-for-multiple-columns-and-saving-predicted-values/m-p/801715#M97751</link>
      <description>&lt;P&gt;It worked amazingly, thank you so much! I've been struggling with ChatGPT to find a solution for hours :D&lt;/img&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Sep 2024 12:10:10 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Fit-Spline-for-multiple-columns-and-saving-predicted-values/m-p/801715#M97751</guid>
      <dc:creator>LGaspard_98</dc:creator>
      <dc:date>2024-09-25T12:10:10Z</dc:date>
    </item>
  </channel>
</rss>

