在 R 中获取图的二次方程项

发布于 2024-09-30 18:51:13 字数 207 浏览 0 评论 0原文

我需要找到我在 R 中绘制的图的二次方程项。 当我在 Excel 中执行此操作时,该术语出现在图表上的文本框中,但我不确定如何将其移动到单元格以供后续使用(以应用于需要校准的值),或者实际上如何在 R 中请求它。如果它在 R 中是可召唤的,它是否可以保存为一个对象以用于将来的计算?

这似乎应该是 R 中的一个简单请求,但我找不到任何类似的问题。非常感谢任何人可以就此提供的任何帮助。

I need to find the quadratic equation term of a graph I have plotted in R.
When I do this in excel, the term appears in a text box on the chart but I'm unsure how to move this to a cell for subsequent use (to apply to values requiring calibrating) or indeed how to ask for it in R. If it is summonable in R, is it saveable as an object to do future calculations with?

This seems like it should be a straightforward request in R, but I can't find any similar questions. Many thanks in advance for any help anyone can provide on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦毁影碎の 2024-10-07 18:51:13

所有的答案都提供了您似乎想做的事情的各个方面,但到目前为止还没有将它们全部整合在一起。让我们考虑 Tom Liptrot 的答案示例:

fit <- lm(speed ~ dist + I(dist^2), cars)

这为我们提供了一个拟合线性模型,变量 dist 中有一个二次方程。我们使用 coef() 提取器函数提取模型系数:

> coef(fit)
 (Intercept)         dist    I(dist^2) 
 5.143960960  0.327454437 -0.001528367

因此您的拟合方程(由于打印而进行舍入):

\hat{speed} = 5.143960960 + (0.327454437 * dist) + (- 0.001528367 * dist^2)

(其中 \hat{speed} 是响应速度的拟合值)。

如果你想将这个拟合方程应用于某些数据,那么我们可以编写自己的函数来完成它:

myfun <- function(newdist, model) {
    coefs <- coef(model)
    res <- coefs[1] + (coefs[2] * newdist) + (coefs[3] * newdist^2)
    return(res)
}

我们可以像这样应用这个函数:

> myfun(c(21,3,4,5,78,34,23,54), fit)
[1] 11.346494  6.112569  6.429325  6.743024 21.386822 14.510619 11.866907
[8] 18.369782

对于一些新的距离值(dist),即但是,在 R 中,我们通常不会做这样的事情,因为,为什么用户必须知道如何从所有不同类型的模型中形成拟合值或预测值?适合R?

在 R 中,我们使用标准方法和提取器函数。在这种情况下,如果您想将 Excel 显示的“方程”应用于所有数据以获得此回归的拟合值,在 R 中我们将使用 fitted() 函数

> fitted(fit)
        1         2         3         4         5         6         7         8 
 5.792756  8.265669  6.429325 11.608229  9.991970  8.265669 10.542950 12.624600 
        9        10        11        12        13        14        15        16 
14.510619 10.268988 13.114445  9.428763 11.081703 12.122528 13.114445 12.624600 
       17        18        19        20        21        22        23        24 
14.510619 14.510619 16.972840 12.624600 14.951557 19.289106 21.558767 11.081703 
       25        26        27        28        29        30        31        32 
12.624600 18.369782 14.057455 15.796751 14.057455 15.796751 17.695765 16.201008 
       33        34        35        36        37        38        39        40 
18.688450 21.202650 21.865976 14.951557 16.972840 20.343693 14.057455 17.340416 
       41        42        43        44        45        46        47        48 
18.038887 18.688450 19.840853 20.098387 18.369782 20.576773 22.333670 22.378377 
       49        50 
22.430008 21.93513

:如果您想将模型方程应用于一些未用于拟合模型的新数据值,那么我们需要从模型中获得预测。这是使用 predict() 函数完成的。使用上面插入到 myfun 中的距离,这就是我们以更以 R 为中心的方式进行操作的方式:

> newDists <- data.frame(dist = c(21,3,4,5,78,34,23,54))
> newDists
  dist
1   21
2    3
3    4
4    5
5   78
6   34
7   23
8   54
> predict(fit, newdata = newDists)
        1         2         3         4         5         6         7         8 
11.346494  6.112569  6.429325  6.743024 21.386822 14.510619 11.866907 18.369782

首先,我们使用名为 “dist”的组件创建一个新数据框,包含我们想要从模型中获得预测的新距离。值得注意的是,我们在此数据框中包含一个与创建拟合模型时使用的变量同名的变量。这一新数据框必须包含用于拟合模型的所有变量,但在本例中我们只有一个变量 dist。另请注意,我们不需要包含有关 dist^2 的任何内容。 R 将为我们处理这个问题。

然后我们使用 predict() 函数,为其提供拟合模型,并提供刚刚创建的新数据框作为参数 'newdata',为我们提供新的预测值,与我们之前手工制作的相匹配。

我掩盖的一点是,predict()fitted() 实际上是一整组函数。有针对 lm() 模型、针对 glm() 模型等的版本。它们被称为通用函数,具有方法< /em>(版本,如果您愿意)适用于几种不同类型的对象。用户通常只需要记住使用 fitted()predict() 等,而 R 会针对您提供的拟合模型类型使用正确的方法。以下是基本 R 中可用于 fitted() 泛型函数的一些方法:

> methods(fitted)
[1] fitted.default*       fitted.isoreg*        fitted.nls*          
[4] fitted.smooth.spline*

   Non-visible functions are asterisked

根据您加载的其他包,您可能会得到更多的方法。 * 只是意味着您不能直接引用这些函数,您必须使用 fitted() 并且 R 会确定要使用哪些函数。请注意,没有针对 lm() 对象的方法。这种类型的对象不需要特殊的方法,因此 default 方法将被使用并且是合适的。

All the answers provide aspects of what you appear at want to do, but non thus far brings it all together. Lets consider Tom Liptrot's answer example:

fit <- lm(speed ~ dist + I(dist^2), cars)

This gives us a fitted linear model with a quadratic in the variable dist. We extract the model coefficients using the coef() extractor function:

> coef(fit)
 (Intercept)         dist    I(dist^2) 
 5.143960960  0.327454437 -0.001528367

So your fitted equation (subject to rounding because of printing is):

\hat{speed} = 5.143960960 + (0.327454437 * dist) + (-0.001528367 * dist^2)

(where \hat{speed} is the fitted values of the response, speed).

If you want to apply this fitted equation to some data, then we can write our own function to do it:

myfun <- function(newdist, model) {
    coefs <- coef(model)
    res <- coefs[1] + (coefs[2] * newdist) + (coefs[3] * newdist^2)
    return(res)
}

We can apply this function like this:

> myfun(c(21,3,4,5,78,34,23,54), fit)
[1] 11.346494  6.112569  6.429325  6.743024 21.386822 14.510619 11.866907
[8] 18.369782

for some new values of distance (dist), Which is what you appear to want to do from the Q. However, in R we don't do things like this normally, because, why should the user have to know how to form fitted or predicted values from all the different types of model that can be fitted in R?

In R, we use standard methods and extractor functions. In this case, if you want to apply the "equation", that Excel displays, to all your data to get the fitted values of this regression, in R we would use the fitted() function:

> fitted(fit)
        1         2         3         4         5         6         7         8 
 5.792756  8.265669  6.429325 11.608229  9.991970  8.265669 10.542950 12.624600 
        9        10        11        12        13        14        15        16 
14.510619 10.268988 13.114445  9.428763 11.081703 12.122528 13.114445 12.624600 
       17        18        19        20        21        22        23        24 
14.510619 14.510619 16.972840 12.624600 14.951557 19.289106 21.558767 11.081703 
       25        26        27        28        29        30        31        32 
12.624600 18.369782 14.057455 15.796751 14.057455 15.796751 17.695765 16.201008 
       33        34        35        36        37        38        39        40 
18.688450 21.202650 21.865976 14.951557 16.972840 20.343693 14.057455 17.340416 
       41        42        43        44        45        46        47        48 
18.038887 18.688450 19.840853 20.098387 18.369782 20.576773 22.333670 22.378377 
       49        50 
22.430008 21.93513

If you want to apply your model equation to some new data values not used to fit the model, then we need to get predictions from the model. This is done using the predict() function. Using the distances I plugged into myfun above, this is how we'd do it in a more R-centric fashion:

> newDists <- data.frame(dist = c(21,3,4,5,78,34,23,54))
> newDists
  dist
1   21
2    3
3    4
4    5
5   78
6   34
7   23
8   54
> predict(fit, newdata = newDists)
        1         2         3         4         5         6         7         8 
11.346494  6.112569  6.429325  6.743024 21.386822 14.510619 11.866907 18.369782

First up we create a new data frame with a component named "dist", containing the new distances we want to get predictions for from our model. It is important to note that we include in this data frame a variable that has the same name as the variable used when we created our fitted model. This new data frame must contain all the variables used to fit the model, but in this case we only have one variable, dist. Note also that we don't need to include anything about dist^2. R will handle that for us.

Then we use the predict() function, giving it our fitted model and providing the new data frame just created as argument 'newdata', giving us our new predicted values, which match the ones we did by hand earlier.

Something I glossed over is that predict() and fitted() are really a whole group of functions. There are versions for lm() models, for glm() models etc. They are known as generic functions, with methods (versions if you like) for several different types of object. You the user generally only need to remember to use fitted() or predict() etc whilst R takes care of using the correct method for the type of fitted model you provide it. Here are some of the methods available in base R for the fitted() generic function:

> methods(fitted)
[1] fitted.default*       fitted.isoreg*        fitted.nls*          
[4] fitted.smooth.spline*

   Non-visible functions are asterisked

You will possibly get more than this depending on what other packages you have loaded. The * just means you can't refer to those functions directly, you have to use fitted() and R works out which of those to use. Note there isn't a method for lm() objects. This type of object doesn't need a special method and thus the default method will get used and is suitable.

不羁少年 2024-10-07 18:51:13

您可以在 lm 的论坛中添加二次项来获得您想要的拟合值。您需要在要平方的项周围使用 I(),如下例所示:

plot(speed ~ dist, cars)

fit1 = lm(speed ~ dist, cars) #fits a linear model
abline(fit1) #puts line on plot
fit2 = lm(speed ~ I(dist^2) + dist, cars) #fits a model with a quadratic term
fit2line = predict(fit2, data.frame(dist = -10:130))
lines(-10:130 ,fit2line, col=2) #puts line on plot

要从此使用中获取系数:

coef(fit2)

You can add a quadratic term in the forumla in lm to get the fit you are after. You need to use an I()around the term you want to square as in the example below:

plot(speed ~ dist, cars)

fit1 = lm(speed ~ dist, cars) #fits a linear model
abline(fit1) #puts line on plot
fit2 = lm(speed ~ I(dist^2) + dist, cars) #fits a model with a quadratic term
fit2line = predict(fit2, data.frame(dist = -10:130))
lines(-10:130 ,fit2line, col=2) #puts line on plot

To get the coefficients from this use:

coef(fit2)
他是夢罘是命 2024-10-07 18:51:13

我认为在 Excel 中这是不可能的,因为它们只提供获取线性回归系数的函数(SLOPEINTERCEPTLINEST)或对于指数级(GROWTHLOGEST),尽管使用 Visual Basic 可能会更幸运。

对于 R,您可以使用 coef 函数提取模型系数:

mdl <- lm(y ~ poly(x,2,raw=T))
coef(mdl) # all coefficients
coef(mdl)[3] # only the 2nd order coefficient

I dont think it is possible in Excel, as they only provide functions to get coefficients for a linear regression (SLOPE, INTERCEPT, LINEST) or for a exponential one (GROWTH, LOGEST), though you may have more luck by using Visual Basic.

As for R you can extract model coefficients using the coef function:

mdl <- lm(y ~ poly(x,2,raw=T))
coef(mdl) # all coefficients
coef(mdl)[3] # only the 2nd order coefficient
迟月 2024-10-07 18:51:13

我猜你的意思是在 Excel 或 R 中绘制 X 与 Y 值,并在 Excel 中使用“添加趋势线”功能。在 R 中,您可以使用 lm 函数 来拟合数据的线性函数,这也为您提供了“r 平方”项(请参阅 链接页面)。

I guess you mean that you plot X vs Y values in Excel or R, and in Excel use the "Add trendline" functionality. In R, you can use the lm function to fit a linear function to your data, and this also gives you the "r squared" term (see examples in the linked page).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文