斜率和截距函数我非常惊讶地发现 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 )
)
);
回复:斜率和截距函数我发现它提供了一种解决方法。 我已经能够进行计算,但不是以一种简单的方式。 我的帖子更多的是功能请求。 缺少上述功能似乎是一个奇怪的遗漏。
回复:斜率和截距函数也许您已经看过它,但如果没有的话,还有一个相关的线程这里。