如何在MATLAB中获得最小二乘回归线的斜率和截距? [英] How to get the slope and intercept of a least squares regression line in MATLAB?

查看:835
本文介绍了如何在MATLAB中获得最小二乘回归线的斜率和截距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很难让MATLAB根据2D散点图泄露斜率和最小二乘回归线的截距.这似乎比实际要容易,但是MATLAB提供的用于回归的所有现有工具都倾向于假定我做的事情比我想做的要复杂.我应该可以从lsline之类的工具中获得它,但是IDE很难在源代码中进行操作.有人知道这样做的快捷方式吗?

I've been having trouble getting MATLAB to divulge the slope and intercept of a least-squares regression line, based on a 2-D scatterplot. This seems like it should be easier than it's turning out to be, but all the existing tools MATLAB provides for regression tend to assume that I'm doing something more complicated than I want to do. I should be able to get it from a facility like lsline, but the IDE is playing hard-to-get with the source code. Does anyone know a quick and dirty way to do this?

推荐答案

您是否只是在尝试根据散点图直观地估算回归?如果不是,则可以使用polyfit()函数来获取估计值.甚至更好的是,只需编写您自己的函数.如果使一列为1,然后将独立轴变量放置到相邻列中,并调用该矩阵X,然后将因变量存储在称为Y的列向量中,则只需计算beta = (X'*X)\(X'*Y). beta的第一个条目给出截距,随后的条目给出回归变量的系数.

Are you only trying to estimate the regression visually, from the scatterplot? If not, you can use the polyfit() function to get your estimates. Or even better, simply write your own function. If you make a column of 1's, and then place your independent axis variables into adjacent columns, and call that matrix X, and you store your dependent variable in a column vector called Y, then just compute beta = (X'*X)\(X'*Y). The first entry of beta gives the intercept, and the subsequent entries give the coefficients of your regression variables.

这篇关于如何在MATLAB中获得最小二乘回归线的斜率和截距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆