如何在 R 中使用 $ 作为输出组件

  • 本文关键字:输出 组件 r function
  • 更新时间 :
  • 英文 :


首先,我的代码运行良好。我只需要能够使用$从 BestSolarData 中调用年份和季节性组件:

BestSolarData$year
BestSolarData$seasonal

我在代码末尾写了这些。我知道的年份来自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

我了解您的问题,您想使用 BestSolarData$year 检索BestSolarDatayear组件。但是BestSolarDataForLoopSine 返回,它本身被命名为 DataPredict 并返回SineFit函数。它似乎是一个向量而不是一个数据帧,所以$在这里不起作用。

您的示例不可重现,这可能有助于您找到解决方案。有关更多详细信息,请参阅此帖子。

最新更新