2016-03-27 18 views
-2

まず、私のコードは完璧に動作します。私は自分のコードの最後にこれらを書かれている

BestSolarData$year 
BestSolarData$seasonal 

:私は単にで$を使用してBestSolarDataのうち、年、季節のコンポーネントを呼び出すことができるようにする必要があります。私が知っている年はBestYearから来て、季節はForLoopSineの機能でBestDataから来ています。

$を使用してコンポーネントにアクセスできるように助けてください。

SineFit <- function (ToBeFitted) 
{ 
    msvector <- as.vector(ToBeFitted) 
    y <- length(ToBeFitted) 
    x <- 1:y 
    MS.nls <- nls(msvector ~ a*sin(((2*pi)/12)*x+b)+c, start=list(a=300, b=0, c=600)) 
    summary(MS.nls) 
    MScoef <- coef(MS.nls) 
    a <- MScoef[1] 
    b <- MScoef[2] 
    c <- MScoef[3] 
    x <- 1:12 
    FittedCurve <- a*sin(((2*pi)/12)*x+b)+c 
    #dev.new() 
    #layout(1:2) 
    #plot(ToBeFitted) 
    #plot(FittedCurve) 
    return (FittedCurve) 
} 

ForLoopSine <- function(PastData, ComparisonData) 
{ 
    w<-start(PastData)[1] 
    t<-end(PastData)[1] 
    BestDiff <- 9999 
    for(i in w:t) 
    { 
     DataWindow <- window(PastData, start=c(i,1), end=c(t,12)) 
     Datapredict <- SineFit(DataWindow) 
     CurrDiff <- norm1diff(Datapredict, ComparisonData) 
     if (CurrDiff < BestDiff) 
     { 
      BestDiff <- CurrDiff 
      BestYear <- i 
      BestData <- Datapredict 
     } 
    } 
    print(BestDiff) 
    print(BestYear) 
    return(BestData) 
} 

RandomFunction <- function(PastData, SeasonalData) 
{ 
    w <- start(PastData)[1] 
    t <- end(PastData)[1] 
    Seasonal.ts <- ts(SeasonalData, st = c(w,1), end = c(t,12), fr = 12) 
    Random <- PastData-Seasonal.ts 
    layout(1:3) 
    plot(SeasonalData) 
    plot(Seasonal.ts) 
    plot(Random) 
    return(Random) 
} 


BestSolarData <- ForLoopSine(MonthlySolarPre2015, MonthlySolar2015) 
RandomComp <- RandomFunction (MonthlySolarPre2015, BestSolarData) 
acf(RandomComp) 
BestSolarData$year 
BestSolarData$seasonal 

答えて

1

限り、私はあなたの問題を理解し、あなたはBestSolarData$yearBestSolarDatayearコンポーネントを取得したいと思います。しかしBestSolarDataForLoopSineによって返され、それ自体はDataPredictとなり、SineFit関数が返されます。それはベクトルと思われ、data.frameではないので、$はここでは動作しません。

あなたの例は再現性がなく、解決策を見つけるのに役立ちます。詳細はpostを参照してください。

+0

$を介して年と季節のコンポーネントにアクセスできるようにコードを変更する必要があります。 –

+0

再現可能な例を追加します。それ以外の場合は複雑になります。 –

関連する問題