5

我有这个数据框:

输入(头(y1,20))

structure(list(time = structure(c(1373256120, 1373256360, 1373256660, 
1373256960, 1373257260, 1373257560, 1373257860, 1373258220, 1373258460, 
1373258760, 1373259060, 1373259360, 1373259660, 1373259960, 1373260260, 
1373260620, 1373260860, 1373261160, 1373261460, 1373261760), class = c("POSIXct", 
"POSIXt"), tzone = "America/New_York"), cpu = c(2.5803, 2.7954, 
3.0855, 2.6414, 2.4603, 2.2053, 3.2352, 2.2437, 2.0264, 1.9006, 
4.331, 2.068, 1.999, 1.8115, 1.8955, 1.7475, 1.8565, 2.1557, 
1.9113, 1.3635)), .Names = c("time", "cpu"), row.names = c(NA, 
20L), class = "data.frame")

我正在尝试建立一个黄土模型,如下所示:

ls<-loess(cpu~time, data=y1)

我收到此错误:

Error in simpleLoess(y, x, w, span, degree, parametric, drop.square, normalize,  : 
  NA/NaN/Inf in foreign function call (arg 2)
In addition: Warning message:
In simpleLoess(y, x, w, span, degree, parametric, drop.square, normalize,  :
  NAs introduced by coercion

我在这里想念什么?

4

2 回答 2

6

您应该将变量转换为数字并确保它们是有限的。

with(y1,{
  ok <- is.finite(time) & is.finite(cpu)
  x <- as.numeric(time)[ok]   ## you reproduce  the error without coercion 
  y <- as.numeric(cpu)[ok]
  data <- list(x = x, y = y)
  loess( y~x,data = data)
})

loess(formula = y ~ x, data = data)
Number of Observations: 20 
Equivalent Number of Parameters: 4.42 
Residual Standard Error: 0.6174 

使用panel.smootherfromlatticeExtra你可以在没有任何操作的情况下得到结果(它应该在内部完成上面的工作)

library(latticeExtra)
xyplot(cpu ~ time ,type='l',data=y1)+
  layer(panel.smoother(y ~ x, span = 0.75),style=2)

在此处输入图像描述

于 2013-07-08T17:25:06.817 回答
0

使用 ggplot() 绘制黄土模型非常容易。这是 geom_smooth() 的默认值。所以你甚至不需要计算它,你可以直接绘制它。

df <- data.frame(time = as.numeric(dput$time), cpu = as.numeric(dput$cpu))

ggplot(df, aes(time, cpu)) + 
   geom_line(aes(colour = "red")) + geom_smooth(aes(colour = "black"))
于 2013-07-10T00:29:32.873 回答