简体   繁体   English

Excel 中的二次和三次回归

[英]Quadratic and cubic regression in Excel

I have the following information:我有以下信息:

  Height    Weight

    170     65
    167     55
    189     85
    175     70
    166     55
    174     55
    169     69
    170     58
    184     84
    161     56
    170     75
    182     68
    167     51
    187     85
    178     62
    173     60
    172     68
    178     55
    175     65
    176     70

I want to construct quadratic and cubic regression analysis in Excel.我想在 Excel 中构建二次和三次回归分析。 I know how to do it by linear regression in Excel, but what about quadratic and cubic?我知道如何通过 Excel 中的线性回归来实现,但是二次和三次呢? I have searched a lot of resources, but could not find anything helpful.我搜索了很多资源,但找不到任何有用的东西。

You need to use an undocumented trick with Excel's LINEST function:您需要在 Excel 的LINEST函数中使用一个未公开的技巧:

=LINEST(known_y's, [known_x's], [const], [stats])

Background背景

A regular linear regression is calculated (with your data) as:常规线性回归计算(使用您的数据)为:

=LINEST(B2:B21,A2:A21)

which returns a single value, the linear slope ( m ) according to the formula:它根据公式返回一个值,即线性斜率 ( m ):

在此处输入图片说明

which for your data:对于您的数据:

在此处输入图片说明

is:是:

在此处输入图片说明

Undocumented trick Number 1未记录的技巧 1

You can also use Excel to calculate a regression with a formula that uses an exponent for x different from 1 , eg x 1.2 :您还可以使用 Excel 使用公式计算回归,该公式使用x的指数不同于1 ,例如 x 1.2

在此处输入图片说明

using the formula:使用公式:

=LINEST(B2:B21, A2:A21^1.2)

which for you data:哪些数据适合您:

在此处输入图片说明

is:是:

在此处输入图片说明

You're not limited to one exponent你不仅限于一个指数

Excel's LINEST function can also calculate multiple regressions, with different exponents on x at the same time, eg: Excel 的LINEST函数还可以计算多个回归,同时在x上使用不同的指数,例如:

=LINEST(B2:B21,A2:A21^{1,2})

Note: if locale is set to European (decimal symbol ","), then comma should be replaced by semicolon and backslash, ie =LINEST(B2:B21;A2:A21^{1\\2})注意:如果 locale 设置为欧洲(十进制符号“,”),那么逗号应该替换为分号和反斜杠,即=LINEST(B2:B21;A2:A21^{1\\2})

Now Excel will calculate regressions using both x 1 and x 2 at the same time:现在 Excel 将同时使用 x 1和 x 2计算回归:

在此处输入图片说明

How to actually do it如何实际操作

The impossibly tricky part there's no obvious way to see the other regression values.不可能的棘手部分没有明显的方法来查看其他回归值。 In order to do that you need to:为此,您需要:

  • select the cell that contains your formula:选择包含公式的单元格:

    在此处输入图片说明

  • extend the selection the left 2 spaces (you need the select to be at least 3 cells wide):将选择扩展到左侧 2 个空格(您需要选择至少有 3 个单元格宽):

    在此处输入图片说明

  • press F2F2

  • press Ctrl + Shift + EnterCtrl + Shift + Enter

    在此处输入图片说明

You will now see your 3 regression constants:您现在将看到 3 个回归常数:

  y = -0.01777539x^2 + 6.864151123x + -591.3531443

Bonus Chatter奖金喋喋不休

I had a function that I wanted to perform a regression using some exponent:我有一个函数,我想使用一些指数执行回归:

y = m×x k + b y = m×x k + b

But I didn't know the exponent.但我不知道指数。 So I changed the LINEST function to use a cell reference instead:因此,我将LINEST函数更改为使用单元格引用:

=LINEST(B2:B21,A2:A21^F3, true, true)

With Excel then outputting full stats (the 4th paramter to LINEST ):然后使用 Excel 输出完整的统计信息( LINEST的第四个参数):

在此处输入图片说明

I tell the Solver to maximize R 2 :我告诉求解器最大化 R 2

在此处输入图片说明

And it can figure out the best exponent.它可以找出最佳指数。 Which for you data:哪些适合您的数据:

在此处输入图片说明

is:是:

在此处输入图片说明

I know that this question is a little old, but I thought that I would provide an alternative which, in my opinion, might be a little easier.我知道这个问题有点老了,但我想我会提供一个替代方案,在我看来,它可能更容易一些。 If you're willing to add "temporary" columns to a data set, you can use Excel's Analysis ToolPak→Data Analysis→Regression.如果您愿意将“临时”列添加到数据集中,您可以使用 Excel 的分析工具库→数据分析→回归。 The secret to doing a quadratic or a cubic regression analysis is defining the Input X Range: .进行二次或三次回归分析的秘诀是定义输入 X 范围:

If you're doing a simple linear regression, all you need are 2 columns, X & Y. If you're doing a quadratic, you'll need X_1, X_2, & Y where X_1 is the x variable and X_2 is x^2 ;如果你在做一个简单的线性回归,你只需要 2 列,X & Y。如果你在做二次方,你需要 X_1、X_2 和 Y,其中 X_1 是x变量,X_2 是x^ 2 ; likewise, if you're doing a cubic, you'll need X_1, X_2, X_3, & Y where X_1 is the x variable, X_2 is x^2 and X_3 is x^3 .同样,如果你在做三次方,你需要 X_1, X_2, X_3, & Y 其中 X_1 是x变量, X_2 是x^2而 X_3 是x^3 Notice how the Input X Range is from A1 to B22, spanning 2 columns.请注意输入 X 范围如何从 A1 到 B22,跨越 2 列。

Excel 中二次回归分析的输入

The following image the output of the regression analysis.下图是回归分析的输出。 I've highlighted the common outputs, including the R-Squared values and all the coefficients.我强调了常见的输出,包括 R 平方值和所有系数。

Excel中二次回归分析的系数

The LINEST function described in a previous answer is the way to go, but an easier way to show the 3 coefficients of the output is to additionally use the INDEX function.前一个答案中描述的 LINEST 函数是可行的方法,但显示输出的 3 个系数的更简单方法是额外使用 INDEX 函数。 In one cell, type: =INDEX(LINEST(B2:B21,A2:A21^{1,2},TRUE,FALSE),1) (by the way, the B2:B21 and A2:A21 I used are just the same values the first poster who answered this used... of course you'd change these ranges appropriately to match your data).在一个单元格中,键入: =INDEX(LINEST(B2:B21,A2:A21^{1,2},TRUE,FALSE),1) (顺便说一下,我使用的 B2:B21 和 A2:A21 只是回答这个问题的第一个发帖人使用了相同的值......当然你会适当地更改这些范围以匹配你的数据)。 This gives the X^2 coefficient.这给出了 X^2 系数。 In an adjacent cell, type the same formula again but change the final 1 to a 2... this gives the X^1 coefficient.在相邻的单元格中,再次键入相同的公式,但将最后的 1 更改为 2...这给出了 X^1 系数。 Lastly, in the next cell over, again type the same formula but change the last number to a 3... this gives the constant.最后,在下一个单元格中,再次输入相同的公式,但将最后一个数字更改为 3...这给出了常量。 I did notice that the three coefficients are very close but not quite identical to those derived by using the graphical trendline feature under the charts tab.我确实注意到这三个系数非常接近,但与使用图表选项卡下的图形趋势线功能得出的系数并不完全相同。 Also, I discovered that LINEST only seems to work if the X and Y data are in columns (not rows), with no empty cells within the range, so be aware of that if you get a #VALUE error.此外,我发现 LINEST 似乎仅在 X 和 Y 数据位于列(而不是行)中且范围内没有空单元格时才起作用,因此如果出现 #VALUE 错误,请注意这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM