-2

First, my code works perfectly. I simply need to be able to call the year and seasonal components out of BestSolarData using $ with:

BestSolarData$year
BestSolarData$seasonal

I have these written at the end of my code. The year I know comes from BestYear and seasonal come from BestData in the ForLoopSine function.

Any help to be able to access the components using $?

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
Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38

1 Answers1

1

As far as I understand your problem, you would like to retrieve the year component of BestSolarData with BestSolarData$year. But BestSolarData is returned by ForLoopSine, which is itself named DataPredict and is returned the SineFit function. It seems to be a vector and not a data.frame, so $ cannot work here.

Your example is not reproducible and this may help you find a solution. See this post for more details.

Community
  • 1
  • 1
Vincent Bonhomme
  • 7,235
  • 2
  • 27
  • 38