Math 计算机绘图实用程序

Math 计算机绘图实用程序,math,r,graph,statistics,jfreechart,Math,R,Graph,Statistics,Jfreechart,我在R中开发了一个系统,用于绘制从风力涡轮机获得的大型数据集。我现在将这个过程移植到Java中。我在两个系统之间得到的结果是不一致的 如下图所示: 首先使用R绘制数据集,然后使用JFreeChart绘制数据集 这两个图中的红线对应于我用每种语言进行的计算(详情如下) #1中的棕色虚线对应于#2中的蓝色线,此处没有差异,仅供参考 阴影区域表示数据点,1为灰色,2为红色。 我可以解释(红色)计算线之间的差异,这是由于我使用了不同的计算方法 在R中,数据处理如下,我写了这段代码,不知道这里发生了

我在R中开发了一个系统,用于绘制从风力涡轮机获得的大型数据集。我现在将这个过程移植到Java中。我在两个系统之间得到的结果是不一致的

如下图所示:

  • 首先使用R绘制数据集,然后使用JFreeChart绘制数据集
  • 这两个图中的红线对应于我用每种语言进行的计算(详情如下)
  • #1中的棕色虚线对应于#2中的蓝色线,此处没有差异,仅供参考
  • 阴影区域表示数据点,1为灰色,2为红色。
我可以解释(红色)计算线之间的差异,这是由于我使用了不同的计算方法

在R中,数据处理如下,我写了这段代码,不知道这里发生了什么(但是,嘿,它起作用了)

我对两个图表之间的差异的看法:

  • 在x_轴上18处出现一个孤立点(在R图上最明显)似乎对曲线的形状有巨大的影响
  • 即使在x轴上的5到15之间,R图中的线似乎更连续,它也不像Java生成的那样容易改变轨迹
  • 爪哇x轴上18处明显的“火山口”必须在其两侧“堆积”,我相信这是由于渲染系统中的多项式效应
这些都是我想消除的东西

因此,为了理解这两张图之间的区别,我有几个问题:

  • 我的R脚本中到底发生了什么
  • 如何将相同的进程移植到Java代码中
  • 谁能解释一下JFreeCharts使用的样条线系统,还有其他的吗
在R代码中,您(在我演示示例时我是这样)将加法模型拟合到功率和速度数据,其中变量之间的关系由数据本身确定。这些模型包括使用样条曲线来估计响应函数。特别是在这里,我使用了一个自适应平滑器,它具有平滑拟合的复杂性
k=20
。越复杂越平滑,拟合函数就越摇摆。自适应平滑器是平滑度随拟合函数变化的平滑器

为什么这很重要?从你的数据来看,有些阶段的响应不随速度变化而变化,有些阶段的响应随速度变化而迅速变化。我们有一个在曲线上消耗的“余量”。对于普通样条曲线,整个函数的wigglyness(或平滑度)是相同的。通过自适应平滑,我们可以在响应变化/变化最大的函数部分使用更多的wigglyness余量,而不必在响应不变的部分使用任何不需要的余量

下面我对代码进行注释,以解释在每个步骤中执行的操作:

## here we create a data frame with the pwr and spd variables
df <- data.frame(pwr = pwr, spd = spd)

## we load the package containing the code to fit the additive model
require(mgcv)

## This is the model itself, saying pwr is modelled as a smooth function of spd
## and the smooth function of spd is generated using an adaptive smoother with
## and "allowance" of 20. This allowance is a starting point and the actual
## smoothness of the curve will be estimated as part of the model fitting,
## here using a REML criterion
mod <- gam(pwr ~ s(spd, bs = "ad", k = 20), data = df, method = "REML")

## This just summarise the model fit
summary(mod)

## In this line we are creating a new spd vector (in a data frame) that contains
## 100 equally spaced spd values over the entire range of the observed spd
x_grid <- with(df, data.frame(spd = seq(min(spd) + 0.0001, maxi, length=100)))

## we will use those data to get predictions of the response pwr at each
## of the 100 values of spd we just created
## I did this so we had enough data to plot a nice smooth curve, but without
## having to predict for all the observed values of spd
pred <- predict(mod, x_grid, se.fit = TRUE)

## This line stores the 100 predicted values in the prediction data object
x_grid <- within(x_grid, fit <- pred$fit)

## This line draws the fitted smooth on to a plot of the data
## this assumes there is already a plot on the active device.
lines(fit ~ spd, data = x_grid, col = "red", lwd = thickLineWidth)
###这里我们创建一个包含pwr和spd变量的数据帧
如果你想知道你的R脚本中到底发生了什么,你需要学习一门关于广义加法模型或非参数平滑的课程。Java中不太可能有这样的代码——您必须自己编写。
SELECT 
    ROUND( ROUND( x_data * 2 ) / 2, 1)   AS x_axis, # See https://stackoverflow.com/questions/5230647/mysql-rounding-functions
    AVG( y_data )                        AS y_axis 
FROM 
    table 
GROUP BY 
    x_axis
## here we create a data frame with the pwr and spd variables
df <- data.frame(pwr = pwr, spd = spd)

## we load the package containing the code to fit the additive model
require(mgcv)

## This is the model itself, saying pwr is modelled as a smooth function of spd
## and the smooth function of spd is generated using an adaptive smoother with
## and "allowance" of 20. This allowance is a starting point and the actual
## smoothness of the curve will be estimated as part of the model fitting,
## here using a REML criterion
mod <- gam(pwr ~ s(spd, bs = "ad", k = 20), data = df, method = "REML")

## This just summarise the model fit
summary(mod)

## In this line we are creating a new spd vector (in a data frame) that contains
## 100 equally spaced spd values over the entire range of the observed spd
x_grid <- with(df, data.frame(spd = seq(min(spd) + 0.0001, maxi, length=100)))

## we will use those data to get predictions of the response pwr at each
## of the 100 values of spd we just created
## I did this so we had enough data to plot a nice smooth curve, but without
## having to predict for all the observed values of spd
pred <- predict(mod, x_grid, se.fit = TRUE)

## This line stores the 100 predicted values in the prediction data object
x_grid <- within(x_grid, fit <- pred$fit)

## This line draws the fitted smooth on to a plot of the data
## this assumes there is already a plot on the active device.
lines(fit ~ spd, data = x_grid, col = "red", lwd = thickLineWidth)