有没有办法将我在函数 poly.calc 的系数中得到的小数舍入?

Is there a way to round a decimals that i get in the coefficients of the function poly.calc?

我正在使用 poly.calc() 函数对一些点进行插值,问题是我需要为每个系数打印最多两位小数的多项式

我知道在 R 中有一个像 round (...) 这样的函数,它可以做到这一点,但是对于数字,我不知道它是否适用于我从 poly.calc 得到的表达式().

一个例子:

library(PolynomF)

x=c(1:4)
y=c(15,12,14,17)

pol = poly.calc(x,y)
pol

这是结果:

27 - 17.83333*x + 6.5*x^2 - 0.6666667*x^3

我需要这样的东西:

27 - 17.83*x + 6.5*x^2 - 0.66*x^3

您将尝试使用以下示例中所示的 round 函数并根据您的需要进行编辑:

# --- calculate parameters lm -----------------------------------------
model <- lm(Pegel$D00 ~ Pegel$dss)
# summary(model)
a = round(as.vector(coef(model)[1]), 2)
b = round(as.vector(coef(model)[2]*365), 2) # scaled for year
r2 = round(summary(model)$r.squared, 2)

我们可以利用 round

library(PolynomF)
pol <- round(pol, 2)
pol
#27 - 17.83*x + 6.5*x^2 - 0.67*x^3 

数据

pol <- poly_calc(x,y)