斜率和截距函數我非常驚訝地發現 JMP 沒有斜率或截距函數。 真的可以這樣嗎?
如果有 Slope(xMatrix, yMatrix)、Intercept 等函數,那就太好了新增了 (xMatrix, yMatrix)、Col Slope(xCol, yCol, By Vraiables) 和 Col Intercept(xCol, yCol, By Vraiables)(以及 R2、SE 等診斷函數),產生簡單的 RMS 線性迴歸係數。
我知道我可以在雙變量中進行回歸,生成斜率等數據表,並用這些數據更新我的原始表,但這不是動態的,對於大量回歸分析來說是不實用的。
希望你們以後能加入這樣的功能。
回覆:斜率和截距函數** 編輯:
基於這篇文章,我在 JMP 14 中加入了 Linear Regression() JSL 函數。 現在,提取斜率和截距的 JSL 更加簡單:
coefs = Linear Regression( y, X )[1]; <BR />coefs[1]; // intercept
coefs[2]; // slope
各種診斷資訊(例如 t 檢定統計量、p 值和 R 平方)也會回傳並(可選)整齊地列印到日誌視窗。請注意,線性迴歸()有能力不僅僅是簡單的線性迴歸。 請參閱腳本索引,以了解使用具有所有可選功能的函數的三個不同範例。
***
傑斯珀,
感謝您的推薦。 這是一個很好的建議,我將考慮在 JMP 的未來版本中添加更好的解決方案。同時,我在 JMP 13 中新增了一個名為「最小二乘求解」的 JSL 函數,它可能對您有用。
在兩行程式碼中,您可以獲得截距、斜率和標準誤差的估計值(不包括擷取結果的程式碼)。它比您所要求的更通用,並且通過一些線性代數知識最容易理解。 它也不會報告「Fit Y by X」等平台中提供的 R^2 值或模型適合診斷。
為了向您展示如何使用最小二乘求解擬合簡單的線性迴歸,我為 x 產生了一個隨機向量,然後產生線性依賴 x 的 y。
// Generating some data
intercept = 2; slope = 4;
x = J(100,1,Random Normal());
y = intercept + x * slope +J (100,1,Random Normal(0,.3));
在 JMP 13 中,最小平方法求解需要設計矩陣,並且不會為您新增截距項。 若要新增截距,您必須為觀察到的 x 新增一列 1。 執行此操作的 JSL 是:
// add an intercept term by adding a column of 1s to the design matrix, X
X.design = J(nrow(x),1) || x;
如果您熟悉線性代數,最小平方法解將求解與模型相關的正規方程式:
y = X.design * beta + 誤差
然而,由於 X.design 有 2 列,第一列是 1,因此模型簡化為簡單線性迴歸,即對於個體 i:
y_i = 截距 + 斜率 * x_i + error_i。
// Solve for intercept, slope, and variance terms for each
{beta, var} = Least Squares Solve(X.design, y);
最小平方法 Solve() 傳回估計的 beta 係數(在本例中為截距和斜率)及其相關的變異數和協方差。 請注意,標準誤差是估計變異數的平方根。 要擷取截距、斜率及其各自的標準誤差,程式碼為:
// To retrieve the intercept, slope, and their associated standard errors:
beta[1]; //the intercept
beta[2]; //the slope
sqrt(var[1,1]); // the standard error for the intercept
sqrt(var[2,2]); // the standard error for the slope
我希望這有幫助,
米洛
回覆:斜率和截距函數它執行我想要的計算,但不是我喜歡的方式。 再說一次,我可以進行計算,所以這不是問題。 我只是認為可能(應該)有一個更簡單的方法。
回覆:斜率和截距函數你好傑斯珀,
你見過嗎用於收集斜率和截距的插件?
我相信這會做你想要的。
乾杯,
史丹
回覆:斜率和截距函數謝謝
我想我在尋找解決方案時也看到了後一個例子。 但我仍然不認為它解決了我的問題。 我的問題不是我無法計算斜率和截距,而是這樣做似乎不必要地麻煩。
在您的範例中,我需要編寫函數腳本,並產生程式碼以從表中提取每個相關副變數的 x 和 y 矩陣。
我的問題不是為了幫助計算。 我只是想表達我對上述內建功能的渴望。 如果它們在那裡,我會經常使用它們。 現在我以同樣的頻率想念他們。
傑斯珀
回覆:斜率和截距函數這JMP 腳本編寫指南有一節關於統計例子在裡面資料結構部分關於矩陣顯示如何使用矩陣運算來計算線性迴歸。
我已經使用該範例建立了您正在尋找的lope() 和intercept() 函數。
slope = Function( {x, y},
{beta},
If( !(Is Matrix( x ) & Is Matrix( y ) & N Col( x ) == 1 & N Col( y ) == 1),
Show( "x or y arguments are not matrices or they have more than one column" );
Throw();
,
x = J( N Row( x ), 1 ) || x; // put in an intercept column of 1s
beta = Inv( X` * X ) * X` * Y; // the least square estimates
beta[2];
)
);
intercept = Function( {x, y},
{beta},
If( !(Is Matrix( x ) & Is Matrix( y ) & N Col( x ) == 1 & N Col( y ) == 1),
Show( "x or y arguments are not matrices or they have more than one column" );
Throw();
,
x = J( N Row( x ), 1 ) || x; // put in an intercept column of 1s
beta = Inv( X` * X ) * X` * Y; // the least square estimates
beta[1];
)
);
Y = [98, 112.5, 84, 102.5, 102.5, 50.5, 90, 77, 112, 150, 128, 133, 85, 112];
X = [65.3, 69, 56.5, 62.8, 63.5, 51.3, 64.3, 56.3, 66.5, 72, 64.8, 67, 57.5, 66.5];
s = slope( x, y );
i = intercept( x, y );
Show( s, i );
這統計例子部分擴展了此範例,以展示如何計算多元迴歸的殘差和其他診斷結果。
我將在下面重複這個例子。
// open the data table
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );
// get data into matrices
x = (Column( "Age" ) << Get Values) || (Column( "Height" ) << Get Values);
x = J( N Row( x ), 1, 1 ) || x;
y = Column( "Weight" ) << Get Values;
// regression calculations
xpxi = Inv( x` * x );
beta = xpxi * x` * y; // parameter estimates
resid = y - x * beta; // residuals
sse = resid` * resid; // sum of squared errors
dfe = N Row( x ) - N Col( x ); // degrees of freedom
mse = sse / dfe; // mean square error, error variance estimate
// additional calculations on estimates
stdb = Sqrt( Vec Diag( xpxi ) * mse ); // standard errors of estimates
alpha = .05;
qt = Students t Quantile( 1 - alpha / 2, dfe );
betau95 = beta + qt * stdb; // upper 95% confidence limits
betal95 = beta - qt * stdb; // lower 95% confidence limits
tratio = beta :\</img> stdb; // Student's T ratios
probt = (1 - t Distribution( Abs( tratio ), dfe )) * 2; // p-values
// present results
New Window( "Big Class Regression",
Table Box(
String Col Box( "Term", {"Intercept", "Age", "Height"} ),
Number Col Box( "Estimate", beta ),
Number Col Box( "Std Error", stdb ),
Number Col Box( "TRatio", tratio ),
Number Col Box( "Prob>|t|", probt ),
Number Col Box( "Lower95%", betal95 ),
Number Col Box( "Upper95%", betau95 )
)
);
回覆:斜率和截距函數我發現它提供了一種解決方法。 我已經能夠進行計算,但不是以簡單的方式。 我的帖子更多的是功能請求。 缺少上述功能似乎是一個奇怪的遺漏。
回覆:斜率和截距函數也許您已經看過它,但如果沒有的話,還有一個相關的線程這裡。