class_docstr: Compute treatment effect whenever the effect is fully mediated by
  another variable (front-door) or when there is an instrument available.
  
  Currently only supports a linear model for the effects.
  
  Supports additional parameters as listed below.
class_docstr_tok: ["Compute","treatment","effect","whenever","the","effect","is","fully","mediated","by","another","variable","(","front-door",")","or","when","there","is","an","instrument","available",".","Currently","only","supports","a","linear","model","for","the","effects",".","Supports","additional","parameters","as","listed","below","."]
docstr: Fits the estimator with data for effect estimation
  :param data: data frame containing the data
  :param treatment: name of the treatment variable
  :param outcome: name of the outcome variable
  :param iv_instrument_name: Name of the specific instrumental variable
      to be used. Needs to be one of the IVs identified in the
      identification step. Default is to use all the IV variables
      from the identification step.
  :param effect_modifiers: Variables on which to compute separate
              effects, or return a heterogeneous effect function. Not all
              methods support this currently.
docstr_tok: ["Fits","the","estimator","with","data","for","effect","estimation",":","param","data",":","data","frame","containing","the","data",":","param","treatment",":","name","of","the","treatment","variable",":","param","outcome",":","name","of","the","outcome","variable",":","param","iv_instrument_name",":","Name","of","the","specific","instrumental","variable","to","be","used",".","Needs","to","be","one","of","the","IVs","identified","in","the","identification","step",".","Default","is","to","use","all","the","IV","variables","from","the","identification","step",".",":","param","effect_modifiers",":","Variables","on","which","to","compute","separate","effects",",","or","return","a","heterogeneous","effect","function",".","Not","all","methods","support","this","currently","."]
code: def fit(
      self,
      data: pd.DataFrame,
      effect_modifier_names: Optional[List[str]] = None,
      **_,
  ):
      """
      Fits the estimator with data for effect estimation
      :param data: data frame containing the data
      :param treatment: name of the treatment variable
      :param outcome: name of the outcome variable
      :param iv_instrument_name: Name of the specific instrumental variable
          to be used. Needs to be one of the IVs identified in the
          identification step. Default is to use all the IV variables
          from the identification step.
      :param effect_modifiers: Variables on which to compute separate
                  effects, or return a heterogeneous effect function. Not all
                  methods support this currently.
      """
      self._set_effect_modifiers(data, effect_modifier_names)
  
      if len(self._target_estimand.treatment_variable) > 1:
          error_msg = (
              str(self.__class__)
              + "cannot handle more than one treatment variable"
          )
          raise Exception(error_msg)
  
      if self._target_estimand.identifier_method == "frontdoor":
          self.logger.debug(
              "Front-door variable used:"
              + ",".join(
                  self._target_estimand.get_frontdoor_variables()
              )
          )
          self._frontdoor_variables_names = (
              self._target_estimand.get_frontdoor_variables()
          )
          if self._frontdoor_variables_names:
              if len(self._frontdoor_variables_names) > 1:
                  raise ValueError(
                      "Only singleton frontdoor variables are supported for estimation using TwoStageRegression."
                  )
              self._frontdoor_variables = data[
                  self._frontdoor_variables_names
              ]
          else:
              self._frontdoor_variables = None
              error_msg = "No front-door variable present. Two stage regression is not applicable"
              self.logger.error(error_msg)
              raise ValueError(error_msg)
      elif self._target_estimand.identifier_method == "mediation":
          self.logger.debug(
              "Mediators used:"
              + ",".join(self._target_estimand.get_mediator_variables())
          )
          self._mediators_names = (
              self._target_estimand.get_mediator_variables()
          )
  
          if self._mediators_names:
              if len(self._mediators_names) > 1:
                  raise ValueError(
                      "Only singleton mediator variables are supported for estimation using TwoStageRegression."
                  )
              self._mediators = data[self._mediators_names]
          else:
              self._mediators = None
              error_msg = "No mediator variable present. Two stage regression is not applicable"
              self.logger.error(error_msg)
              raise ValueError(error_msg)
      elif self._target_estimand.identifier_method == "iv":
          self.logger.debug(
              "Instrumental variables used:"
              + ",".join(
                  self._target_estimand.get_instrumental_variables()
              )
          )
          self._instrumental_variables_names = (
              self._target_estimand.get_instrumental_variables()
          )
  
          if self._instrumental_variables_names:
              self._instrumental_variables = data[
                  self._instrumental_variables_names
              ]
          else:
              self._instrumental_variables = None
              error_msg = "No instrumental variable present. Two stage regression is not applicable"
              self.logger.error(error_msg)
  
      if self._target_estimand.identifier_method == "frontdoor":
          self._first_stage_model._target_estimand.outcome_variable = (
              parse_state(self._frontdoor_variables_names)
          )
      elif self._target_estimand.identifier_method == "mediation":
          self._first_stage_model._target_estimand.outcome_variable = (
              parse_state(self._mediators_names)
          )
  
      self._first_stage_model.fit(
          data,
          effect_modifier_names=effect_modifier_names,
      )
  
      if self._target_estimand.identifier_method == "frontdoor":
          self._second_stage_model._target_estimand.treatment_variable = parse_state(
              self._frontdoor_variables_names
          )
      elif self._target_estimand.identifier_method == "mediation":
          self._second_stage_model._target_estimand.treatment_variable = parse_state(
              self._mediators_names
          )
  
      self._second_stage_model.fit(
          data,
          effect_modifier_names=effect_modifier_names,
      )
  
      if (
          self._target_estimand.estimand_type
          == EstimandType.NONPARAMETRIC_NDE
      ):
          self._second_stage_model_nde._target_estimand.identifier_method = (
              "backdoor"
          )
          self._second_stage_model_nde.fit(
              data,
              effect_modifier_names=effect_modifier_names,
          )
  
      return self
  
file_path: dowhy/dowhy/causal_estimators/two_stage_regression_estimator.py
imports: import copy
  from typing import Any, List, Optional, Type, Union
  import numpy
  import pandas
  from dowhy.causal_estimator import CausalEstimate, CausalEstimator
  from dowhy.causal_estimators.linear_regression_estimator import LinearRegressionEstimator
  from dowhy.causal_identifier import EstimandType, IdentifiedEstimand
  from dowhy.utils.api import parse_state
label_desc: Use image node_id 2 for calling the TwoStageRegressionEstimator obj's underlying member method code with example usage: obj.fit(data, effect_modifier_names) and returns: self