Index: branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs
===================================================================
--- branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs	(revision 16030)
+++ branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs	(revision 16031)
@@ -58,29 +58,23 @@
     private const string DataPartitionParameterName = "DataPartition";
 
-    public IFixedValueParameter<EnumValue<ReplacementMethodEnum>> ReplacementParameter
-    {
+    public IFixedValueParameter<EnumValue<ReplacementMethodEnum>> ReplacementParameter {
       get { return (IFixedValueParameter<EnumValue<ReplacementMethodEnum>>)Parameters[ReplacementParameterName]; }
     }
-    public IFixedValueParameter<EnumValue<FactorReplacementMethodEnum>> FactorReplacementParameter
-    {
+    public IFixedValueParameter<EnumValue<FactorReplacementMethodEnum>> FactorReplacementParameter {
       get { return (IFixedValueParameter<EnumValue<FactorReplacementMethodEnum>>)Parameters[FactorReplacementParameterName]; }
     }
-    public IFixedValueParameter<EnumValue<DataPartitionEnum>> DataPartitionParameter
-    {
+    public IFixedValueParameter<EnumValue<DataPartitionEnum>> DataPartitionParameter {
       get { return (IFixedValueParameter<EnumValue<DataPartitionEnum>>)Parameters[DataPartitionParameterName]; }
     }
 
-    public ReplacementMethodEnum ReplacementMethod
-    {
+    public ReplacementMethodEnum ReplacementMethod {
       get { return ReplacementParameter.Value.Value; }
       set { ReplacementParameter.Value.Value = value; }
     }
-    public FactorReplacementMethodEnum FactorReplacementMethod
-    {
+    public FactorReplacementMethodEnum FactorReplacementMethod {
       get { return FactorReplacementParameter.Value.Value; }
       set { FactorReplacementParameter.Value.Value = value; }
     }
-    public DataPartitionEnum DataPartition
-    {
+    public DataPartitionEnum DataPartition {
       get { return DataPartitionParameter.Value.Value; }
       set { DataPartitionParameter.Value.Value = value; }
@@ -152,9 +146,10 @@
      FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best,
      Func<double, string, bool> progressCallback = null) {
-
-      IEnumerable<double> targetValues;
-      double originalCalculatorValue = -1;
-
-      PrepareData(rows, problemData, estimatedValues, out targetValues, out originalCalculatorValue);
+      //Calculate original quality-values (via calculator, default is R²)
+      OnlineCalculatorError error;
+      IEnumerable<double> targetValuesPartition = rows.Select(v => problemData.TargetVariableValues.ElementAt(v));
+      IEnumerable<double> estimatedValuesPartition = rows.Select(v => estimatedValues.ElementAt(v));
+      var originalCalculatorValue = CalculateVariableImpact(targetValuesPartition, estimatedValuesPartition, out error);
+      if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation.");
 
       var impacts = new Dictionary<string, double>();
@@ -171,5 +166,5 @@
           if (progressCallback((double)curIdx / count, string.Format("Calculating impact for variable {0} ({1} of {2})", inputVariable, curIdx, count))) { return null; }
         }
-        impacts[inputVariable] = CalculateImpact(inputVariable, model, problemData.Dataset, rows, targetValues, originalCalculatorValue, replacementMethod, factorReplacementMethod);
+        impacts[inputVariable] = CalculateImpact(inputVariable, model, problemData.Dataset, rows, targetValuesPartition, originalCalculatorValue, replacementMethod, factorReplacementMethod);
       }
 
@@ -202,7 +197,7 @@
       // calculate impacts for double variables
       if (dataset.VariableHasType<double>(variableName)) {
-        impact = CalculateImpactForDouble(variableName, model, modifiableDataset, rows, targetValues, originalValue, replacementMethod);
+        impact = CalculateImpactForNumericalVariables(variableName, model, modifiableDataset, rows, targetValues, originalValue, replacementMethod);
       } else if (dataset.VariableHasType<string>(variableName)) {
-        impact = CalculateImpactForString(variableName, model, dataset, modifiableDataset, rows, targetValues, originalValue, factorReplacementMethod);
+        impact = CalculateImpactForFactorVariables(variableName, model, dataset, modifiableDataset, rows, targetValues, originalValue, factorReplacementMethod);
       } else {
         throw new NotSupportedException("Variable not supported");
@@ -221,10 +216,10 @@
       targetValues = rows.Select(v => targetVariableValueList.ElementAt(v));
       var estimatedValuesPartition = rows.Select(v => estimatedValues.ElementAt(v));
-      originalValue = CalculateValue(targetValues, estimatedValuesPartition, out error);
+      originalValue = CalculateVariableImpact(targetValues, estimatedValuesPartition, out error);
 
       if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation.");
     }
 
-    private static double CalculateImpactForDouble(string variableName,
+    private static double CalculateImpactForNumericalVariables(string variableName,
       IRegressionModel model,
       ModifiableDataset modifiableDataset,
@@ -234,11 +229,11 @@
       ReplacementMethodEnum replacementMethod) {
       OnlineCalculatorError error;
-      var newEstimates = EvaluateModelWithReplacedVariable(model, variableName, modifiableDataset, rows, replacementMethod);
-      var newValue = CalculateValue(targetValues, newEstimates, out error);
+      var newEstimates = GetReplacedValuesForNumericalVariables(model, variableName, modifiableDataset, rows, replacementMethod);
+      var newValue = CalculateVariableImpact(targetValues, newEstimates, out error);
       if (error != OnlineCalculatorError.None) { throw new InvalidOperationException("Error during calculation with replaced inputs."); }
       return originalValue - newValue;
     }
 
-    private static double CalculateImpactForString(string variableName,
+    private static double CalculateImpactForFactorVariables(string variableName,
       IRegressionModel model,
       IDataset problemData,
@@ -255,6 +250,6 @@
         foreach (var repl in problemData.GetStringValues(variableName, rows).Distinct()) {
           var originalValues = modifiableDataset.GetReadOnlyStringValues(variableName).ToList();
-          var newEstimates = EvaluateModelWithReplacedVariable(originalValues, model, variableName, modifiableDataset, rows, Enumerable.Repeat(repl, problemData.Rows).ToList());
-          var newValue = CalculateValue(targetValues, newEstimates, out error);
+          var newEstimates = GetReplacedValues(originalValues, model, variableName, modifiableDataset, rows, Enumerable.Repeat(repl, problemData.Rows).ToList());
+          var newValue = CalculateVariableImpact(targetValues, newEstimates, out error);
           if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation with replaced inputs.");
 
@@ -266,6 +261,6 @@
         // for replacement methods shuffle and mode
         // calculate impacts for factor variables
-        var newEstimates = EvaluateModelWithReplacedVariable(model, variableName, modifiableDataset, rows, factorReplacementMethod);
-        var newValue = CalculateValue(targetValues, newEstimates, out error);
+        var newEstimates = GetReplacedValuesForFactorVariables(model, variableName, modifiableDataset, rows, factorReplacementMethod);
+        var newValue = CalculateVariableImpact(targetValues, newEstimates, out error);
         if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during calculation with replaced inputs.");
 
@@ -274,5 +269,10 @@
     }
 
-    private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, ModifiableDataset dataset, IEnumerable<int> rows, ReplacementMethodEnum replacement = ReplacementMethodEnum.Shuffle) {
+    private static IEnumerable<double> GetReplacedValuesForNumericalVariables(
+      IRegressionModel model,
+      string variable,
+      ModifiableDataset dataset,
+      IEnumerable<int> rows,
+      ReplacementMethodEnum replacement = ReplacementMethodEnum.Shuffle) {
       var originalValues = dataset.GetReadOnlyDoubleValues(variable).ToList();
       double replacementValue;
@@ -318,11 +318,13 @@
       }
 
-      return EvaluateModelWithReplacedVariable(originalValues, model, variable, dataset, rows, replacementValues);
-    }
-
-    private static IEnumerable<double> EvaluateModelWithReplacedVariable(
-      IRegressionModel model, string variable, ModifiableDataset dataset,
-      IEnumerable<int> rows,
-      FactorReplacementMethodEnum replacement = FactorReplacementMethodEnum.Best) {
+      return GetReplacedValues(originalValues, model, variable, dataset, rows, replacementValues);
+    }
+
+    private static IEnumerable<double> GetReplacedValuesForFactorVariables(
+      IRegressionModel model,
+      string variable,
+      ModifiableDataset dataset,
+      IEnumerable<int> rows,
+      FactorReplacementMethodEnum replacement = FactorReplacementMethodEnum.Shuffle) {
       var originalValues = dataset.GetReadOnlyStringValues(variable).ToList();
       List<string> replacementValues;
@@ -354,9 +356,14 @@
       }
 
-      return EvaluateModelWithReplacedVariable(originalValues, model, variable, dataset, rows, replacementValues);
-    }
-
-    private static IEnumerable<double> EvaluateModelWithReplacedVariable(IList originalValues, IRegressionModel model, string variable,
-      ModifiableDataset dataset, IEnumerable<int> rows, IList replacementValues) {
+      return GetReplacedValues(originalValues, model, variable, dataset, rows, replacementValues);
+    }
+
+    private static IEnumerable<double> GetReplacedValues(
+      IList originalValues,
+      IRegressionModel model,
+      string variable,
+      ModifiableDataset dataset,
+      IEnumerable<int> rows,
+      IList replacementValues) {
       dataset.ReplaceVariable(variable, replacementValues);
       //mkommend: ToList is used on purpose to avoid lazy evaluation that could result in wrong estimates due to variable replacements
@@ -367,5 +374,5 @@
     }
 
-    private static double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
+    private static double CalculateVariableImpact(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
       IEnumerator<double> firstEnumerator = originalValues.GetEnumerator();
       IEnumerator<double> secondEnumerator = estimatedValues.GetEnumerator();
