Exemplo n.º 1
0
    def fit(self, df, group=None):
        """
        Fits a loose, tight, beta, and p combinations model. If you pass in
        update group it will override the initial parameters with new
        initial parameters based on the df you pass.

        Args:
            df:
            group: (str) passing in the group will update the initialization
                dictionary (not replacing the old one) for this particular fit.

        Returns:

        """
        if group is not None:
            init_dict = self.update_init_model(df=df, group=group)
        else:
            init_dict = deepcopy(self.init_dict)

        for param in ['beta', 'p']:
            if getattr(self, f'{param}_weight') == 0:
                continue
            for fit_type in ['loose', 'tight']:
                model_arg_dict = deepcopy(getattr(self, f'{param}_model_kwargs'))
                fit_arg_dict = deepcopy(getattr(self, f'{fit_type}_{param}_fit_dict'))
                model = CurveModel(df=df, **model_arg_dict)

                fe_init, re_init = compute_starting_params(
                    init_dict[param][fit_type]
                )

                fit_arg_dict.update(fe_init=fe_init, re_init=re_init)
                model.fit_params(**fit_arg_dict)

                setattr(self, f'{fit_type}_{param}_model', model)
Exemplo n.º 2
0
    def fit(self, df, group=None):
        """
        Fits a loose, tight, beta, and p combinations model. If you pass in
        update group it will override the initial parameters with new
        initial parameters based on the df you pass.

        Args:
            df:
            group: (str) passing in the group will update the initialization
                dictionary (not replacing the old one) for this particular fit.

        Returns:

        """
        if group is not None:
            init_dict = self.update_init_model(df=df, group=group)
        else:
            init_dict = deepcopy(self.init_dict)

        fit_dict = deepcopy(self.fit_dict)
        fe_init, re_init = compute_starting_params(init_dict)
        fit_dict.update(fe_init=fe_init, re_init=re_init)

        self.mod = CurveModel(df=df, **self.basic_model_dict)
        self.mod.fit_params(**fit_dict)
Exemplo n.º 3
0
    def get_init_dict(self, df, groups):
        """
        Run the init model for each location.

        Args:
            df: (pd.DataFrame) data frame to fit the model that will
                be subset by group
            groups: (str) groups to get in the dict

        Returns:
            (dict) dictionary of fixed effects keyed by group
        """
        init_dict = {}
        for param in ['beta', 'p']:
            init_dict[param] = {}

            for fit_type in ['loose', 'tight']:
                model_arg_dict = deepcopy(getattr(self, f'{param}_model_kwargs'))
                model = CurveModel(df=df, **model_arg_dict)

                fit_arg_dict = deepcopy(getattr(self, f'{fit_type}_{param}_fit_dict'))
                fit_arg_dict.update(options=self.smart_init_options)

                init_dict[param][fit_type] = get_initial_params(
                    groups=groups,
                    model=model,
                    fit_arg_dict=fit_arg_dict
                )
        return init_dict
Exemplo n.º 4
0
    def run_model(self, df, group):
        """Run each individual model.
        """
        model = CurveModel(df=df[df[self.col_group] == group].copy(),
                           **self.basic_model_dict)

        fit_dict = deepcopy(self.fit_dict)
        fe_gprior = fit_dict['fe_gprior']
        fe_gprior[1][1] *= self.prior_modifier(model.num_obs)
        print(group)
        print('\t update beta fe_gprior to', fe_gprior)

        fit_dict.update({'fe_gprior': fe_gprior})
        model.fit_params(**fit_dict)
        return model
Exemplo n.º 5
0
    def run_model(self, group, **fit_kwargs):
        """Construct and run the model.
        """
        model = CurveModel(
            self.df[self.df[self.col_group] == group].copy(),
            col_t=self.col_t,
            col_obs=self.col_obs,
            col_covs=self.col_covs,
            col_group=self.col_group,
            param_names=self.param_names,
            link_fun=self.link_fun,
            var_link_fun=self.var_link_fun,
            fun=self.fun,
            col_obs_se=self.col_obs_se
        )

        model.fit_params(**fit_kwargs)

        return model
Exemplo n.º 6
0
    def get_init_dict(self, df, groups):
        """
        Run the init model for each location.

        Args:
            df: (pd.DataFrame) data frame to fit the model that will
                be subset by group
            groups: (str) groups to get in the dict

        Returns:
            (dict) dictionary of fixed effects keyed by group
        """
        model = CurveModel(df=df, **self.basic_model_dict)

        init_fit_dict = deepcopy(self.fit_dict)
        init_fit_dict.update(options=self.smart_init_options)

        init_dict = get_initial_params(groups=groups,
                                       model=model,
                                       fit_arg_dict=init_fit_dict)
        return init_dict
Exemplo n.º 7
0
 def fit(self, df, group=None):
     self.mod = CurveModel(df=df, **self.basic_model_dict)
     self.mod.fit_params(**self.fit_dict)
Exemplo n.º 8
0
    def run_joint_model(self, df, groups):
        model = CurveModel(df=df[df[self.col_group].isin(groups)].copy(),
                           **self.basic_model_dict)
        model.fit_params(**self.joint_model_fit_dict)

        return model