平滑r中的图

时间:2014-03-20 14:09:46

标签: r plot

我有一个时间序列。如果我画这个时间序列我有这样的图表

enter image description here

我的数据:

539 532 531 538 544 554 575 571 543 559 511 525 512 540
535 514 524 527 532 547 564 548 572 564 549 532 519 520
520 543 550 542 528 523 531 548 554 574 575 560 534 518
511 519 527 554 543 527 540 524 523 539 569 552 553 540
522 522 492 519 532 527 532 550 535 517 551 548 571 574
539 535 515 512 510 527 533 543 540 533 519 539 555 542
574 543 555 539 507 522 518 519 516 546 523 530 532 539
540 568 554 563 550 526 509 492 525 519 527 526 515 530
531 553 563 562 576 568 539 516 512 500 516 542 522 527
523 531

如何平滑此图表,更清楚地看到sin函数

2 个答案:

答案 0 :(得分:3)

以下是一些可以帮助您入门的事情。

df <- data.frame(index=1:length(values),values)
# loess smoothing; note the use of predict(fit)
fit.loess <- loess(values~index,df,span=.1)
plot(df, type="l", col="blue",main="loess")
lines(df$index,predict(fit.loess),col="red")

# non-linear regression usign a single sine term
fit.nls <- nls(values~a*sin(b*index+c)+d,df,
           start=c(a=1000,b=pi/10,c=0,d=mean(df$values)))
plot(df, type="l", col="blue",main="sin [1 term]")
lines(df$index,predict(fit.nls),col="red")

# non-linear regression using 2 sine terms
fit.nls <- nls(values~a1*sin(b1*index+c1)+a2*sin(b2*index+c2)+d,df,
               start=c(a1=1000,b1=pi/10,c1=1,
                       a2=1000,b2=pi/2,c2=1,d=mean(df$values)))
plot(df, type="l", col="blue",main="sin [2 terms]")
lines(df$index,predict(fit.nls),col="red")

从非线性拟合中,您可以使用b估算期间(summary(fit.nls))。

阅读loessnlspredict

上的文档

答案 1 :(得分:2)

您可以使用任何R包中的平滑功能。基本上,您可以执行ARIMA模型等移动平均功能。

这个场景非常容易探索(我希望这可以帮到你):

#Read the data

cd4Data <- read.table("./RData/cd4.data",  col.names=c("time", "cd4", "age", "packs", "drugs", "sex", "cesd", "id"))

cd4Data <- cd4Data[order(cd4Data$time),]

head(cd4Data)

#Plot the data
par(mfrow=c(1,1))

plot(cd4Data$time,cd4Data$cd4,pch=19,cex=0.1)

#A moving average (With 3 points average)
plot(cd4Data$time,cd4Data$cd4,pch=19,cex=0.1)

aveTime <- aveCd4 <- rep(NA,length(3:(dim(cd4Data)[1]-2)))

for(i in 3:(dim(cd4Data)[1]-2)){

    aveTime[i] <- mean(cd4Data$time[(i-2):(i+2)])

    aveCd4[i] <- mean(cd4Data$cd4[(i-2):(i+2)])

}


lines(aveTime,aveCd4,col="blue",lwd=3)

#Average many more points

plot(cd4Data$time,cd4Data$cd4,pch=19,cex=0.1)

aveTime <- aveCd4 <- rep(NA,length(201:(dim(cd4Data)[1]-200)))

for(i in 201:(dim(cd4Data)[1]-2)){

    aveTime[i] <- mean(cd4Data$time[(i-200):(i+200)])

    aveCd4[i] <- mean(cd4Data$cd4[(i-200):(i+200)])

}

lines(aveTime,aveCd4,col="blue",lwd=3)