purrr auto.arima xreg组合

时间:2018-08-30 11:41:08

标签: r nested purrr forecast

我想基于purrr和预测包构建许多auto.arima模型。 我无法完成下面的代码,出现一些错误。

如果有必要,我们可以在没有可复制代码的情况下开始。

我的数据:

head(df)
nam period     sv
APA 2016-07-03 1895619
APA 2016-07-10 2100690
APA 2016-07-17 2059273
APA 2016-07-24 2073187
APA 2016-07-31 1951968

以及我在R中的代码要完成...

df %>% 
nest(-nam) %>% 
mutate(ts_data = map(data, tk_ts, select = sv, start = c(2016,26), frequency = 52)) %>% 
mutate(harmonics = map(ts_data, fourier, K=24)) %>% 
mutate(fitted = map2(.x = ts_data, .y =harmonics, .f= auto.arima, xreg , seasonal = F)) 

我想实现等效于以下代码:

harmonics <- fourier(db, K = 24)
# Fit regression model with ARIMA errors
fit <- auto.arima(db, xreg = harmonics, seasonal = F)
# Forecasts next 46 periods
newharmonics <- fourier(db, K = 24, h = 46)
fc <- forecast(fit, xreg = newharmonics )

有人可以帮我完成吗? 提前感谢

1 个答案:

答案 0 :(得分:0)

我解决了。只需将其放入公式即可

export const updateScheduleMutation = gql`
  mutation updateScheduleMutation(
    $id: ID!
    $mode: String
  ) {
    updateSchedule(
      id: $id
      mode: $mode
    ) {
      id
      mode
    }
  }
`;

class EditorWrapper extends React.Component {
    constructor(props) {
        super(props);
            this.state = { scheduleId: props.scheduleId || '' };
    }

    render() {
        return (
            <Mutation mutation={updateScheduleMutation}>
              {(updateSchedule, { mutationData }) => <Editor {...data} updateSchedule={updateSchedule} />}
            </Mutation>
        )
    }
}

class Editor extends React.Component {
  constructor(props) {
    super(props);
    const { schedule } = props;
    if(schedule === null){
        this.state = {
        schedule: { mode: schedule.mode || "" }
        };
    }
  }

  componentDidUpdate(prevProps) {
    if (prevProps.schedule !== this.props.schedule) {
      this.setState({ ...this.props.schedule });
    }
  }

  changeInput = (path, input) => {
    const { updateSchedule, schedule } = this.props;
    const field = path.split('.')[1];
    updateSchedule({ variables: { id: schedule.id, [field]: input } });
    this.setState({ [path]: input });
  };

  render() {
    return (
        <ModeField input={this.state.schedule.input} />
    );
  }
}

const ModeField = ({input}) => FormControl value={input} />

` 然后:

`auto_arima = function(df) { harmonics <- fourier(df, K = 24) # Fit regression model with ARIMA errors fit <- auto.arima(df, xreg = harmonics, seasonal = FALSE) # Forecasts next 46 period newharmonics <- fourier(df, K = 24, h = 46) fc <- forecast(fit, xreg = newharmonics) fc_db = fc %>% as_data_frame() %>% select(`Point Forecast`) %>% mutate(period = seq.Date(as.Date("2017-10-15"), as.Date("2018-08-27"),by = "week")) return(fc_db) }

Sewe