Index: /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj
===================================================================
--- /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj	(revision 16388)
+++ /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis-3.4.csproj	(revision 16389)
@@ -263,4 +263,5 @@
     <Compile Include="Linear\MultinomialLogitClassification.cs" />
     <Compile Include="Linear\MultinomialLogitClassificationSolution.cs" />
+    <Compile Include="Linear\LinearRegressionModel.cs" />
     <Compile Include="Linear\MultinomialLogitModel.cs" />
     <Compile Include="Linear\Scaling.cs" />
Index: /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs
===================================================================
--- /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs	(revision 16388)
+++ /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs	(revision 16389)
@@ -62,5 +62,5 @@
     protected override void Run(CancellationToken cancellationToken) {
       double rmsError, cvRmsError;
-      var solution = CreateLinearRegressionSolution(Problem.ProblemData, out rmsError, out cvRmsError);
+      var solution = CreateSolution(Problem.ProblemData, out rmsError, out cvRmsError);
       Results.Add(new Result(LinearRegressionModelResultName, "The linear regression solution.", solution));
       Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the linear regression solution on the training set.", new DoubleValue(rmsError)));
@@ -68,18 +68,10 @@
     }
 
+    [Obsolete("Use CreateSolution() instead")]
     public static ISymbolicRegressionSolution CreateLinearRegressionSolution(IRegressionProblemData problemData, out double rmsError, out double cvRmsError) {
-      var dataset = problemData.Dataset;
-      string targetVariable = problemData.TargetVariable;
-      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
-      IEnumerable<int> rows = problemData.TrainingIndices;
-      var doubleVariables = allowedInputVariables.Where(dataset.VariableHasType<double>);
-      var factorVariableNames = allowedInputVariables.Where(dataset.VariableHasType<string>);
-      var factorVariables = dataset.GetFactorVariableValues(factorVariableNames, rows);
-      double[,] binaryMatrix = dataset.ToArray(factorVariables, rows);
-      double[,] doubleVarMatrix = dataset.ToArray(doubleVariables.Concat(new string[] { targetVariable }), rows);
-      var inputMatrix = binaryMatrix.HorzCat(doubleVarMatrix);
-
-      if (inputMatrix.ContainsNanOrInfinity())
-        throw new NotSupportedException("Linear regression does not support NaN or infinity values in the input dataset.");
+      IEnumerable<string> doubleVariables;
+      IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables;
+      double[,] inputMatrix;
+      PrepareData(problemData, out inputMatrix, out doubleVariables, out factorVariables);
 
       alglib.linearmodel lm = new alglib.linearmodel();
@@ -87,5 +79,4 @@
       int nRows = inputMatrix.GetLength(0);
       int nFeatures = inputMatrix.GetLength(1) - 1;
-      double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant
 
       int retVal = 1;
@@ -95,7 +86,8 @@
       cvRmsError = ar.cvrmserror;
 
+      double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant
       alglib.lrunpack(lm, out coefficients, out nFeatures);
-
-      int nFactorCoeff = binaryMatrix.GetLength(1);
+      
+      int nFactorCoeff = factorVariables.Sum(kvp=>kvp.Value.Count());
       int nVarCoeff = doubleVariables.Count();
       var tree = LinearModelToTreeConverter.CreateTree(factorVariables, coefficients.Take(nFactorCoeff).ToArray(),
@@ -108,4 +100,54 @@
       return solution;
     }
+
+    public static IRegressionSolution CreateSolution(IRegressionProblemData problemData, out double rmsError, out double cvRmsError) {
+      IEnumerable<string> doubleVariables;
+      IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables;
+      double[,] inputMatrix;
+      PrepareData(problemData, out inputMatrix, out doubleVariables, out factorVariables);
+
+      alglib.linearmodel lm = new alglib.linearmodel();
+      alglib.lrreport ar = new alglib.lrreport();
+      int nRows = inputMatrix.GetLength(0);
+      int nFeatures = inputMatrix.GetLength(1) - 1;
+
+      int retVal = 1;
+      alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
+      if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression solution");
+      rmsError = ar.rmserror;
+      cvRmsError = ar.cvrmserror;
+
+      // get parameters of the model
+      double[] w;
+      int nVars;
+      alglib.lrunpack(lm, out w, out nVars);
+
+      // ar.c is the covariation matrix,  array[0..NVars,0..NVars].
+      // C[i, j] = Cov(A[i], A[j])
+
+      var solution = new LinearRegressionModel(w, ar.c, cvRmsError, problemData.TargetVariable, doubleVariables, factorVariables)
+        .CreateRegressionSolution((IRegressionProblemData)problemData.Clone());
+      solution.Name = "Linear Regression Solution";
+      return solution;
+    }
+
+    private static void PrepareData(IRegressionProblemData problemData, 
+      out double[,] inputMatrix, 
+      out IEnumerable<string> doubleVariables, 
+      out IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables) {
+      var dataset = problemData.Dataset;
+      string targetVariable = problemData.TargetVariable;
+      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
+      IEnumerable<int> rows = problemData.TrainingIndices;
+      doubleVariables = allowedInputVariables.Where(dataset.VariableHasType<double>);
+      var factorVariableNames = allowedInputVariables.Where(dataset.VariableHasType<string>);
+      factorVariables = dataset.GetFactorVariableValues(factorVariableNames, rows);
+      double[,] binaryMatrix = dataset.ToArray(factorVariables, rows);
+      double[,] doubleVarMatrix = dataset.ToArray(doubleVariables.Concat(new string[] { targetVariable }), rows);
+      inputMatrix = binaryMatrix.HorzCat(doubleVarMatrix);
+
+      if (inputMatrix.ContainsNanOrInfinity())
+        throw new NotSupportedException("Linear regression does not support NaN or infinity values in the input dataset.");
+    }
     #endregion
   }
Index: /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegressionModel.cs
===================================================================
--- /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegressionModel.cs	(revision 16389)
+++ /trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegressionModel.cs	(revision 16389)
@@ -0,0 +1,146 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using HeuristicLab.Common;
+using HeuristicLab.Core;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Problems.DataAnalysis;
+
+namespace HeuristicLab.Algorithms.DataAnalysis {
+  /// <summary>
+  /// Represents a linear regression model
+  /// </summary>
+  [StorableClass]
+  [Item("Linear Regression Model", "Represents a linear regression model.")]
+  public sealed class LinearRegressionModel : RegressionModel, IConfidenceRegressionModel {
+
+    [Storable]
+    public double[,] C {
+      get; private set;
+    }
+    [Storable]
+    public double[] W {
+      get; private set;
+    }
+
+    [Storable]
+    public double NoiseSigma {
+      get; private set;
+    }
+    
+    public override IEnumerable<string> VariablesUsedForPrediction {
+      get { return allowedInputVariables; }
+    }
+
+    [Storable]
+    private string[] allowedInputVariables;
+    [Storable]
+    private List<KeyValuePair<string, IEnumerable<string>>> factorVariables;
+
+    [StorableConstructor]
+    private LinearRegressionModel(bool deserializing)
+      : base(deserializing) {
+    }
+    private LinearRegressionModel(LinearRegressionModel original, Cloner cloner)
+      : base(original, cloner) {
+      this.W = original.W;
+      this.C = original.C;
+      this.NoiseSigma = original.NoiseSigma;
+
+      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
+      this.factorVariables = original.factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
+    }
+    public LinearRegressionModel(double[] w, double[,] covariance, double noiseSigma, string targetVariable, IEnumerable<string> doubleInputVariables, IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables)
+      : base(targetVariable) {
+      this.name = ItemName;
+      this.description = ItemDescription;
+      this.W = new double[w.Length];
+      Array.Copy(w, W, w.Length);
+      this.C = new double[covariance.GetLength(0),covariance.GetLength(1)];
+      Array.Copy(covariance, C, covariance.Length);
+      this.NoiseSigma = noiseSigma;
+      this.allowedInputVariables = doubleInputVariables.ToArray();
+      this.factorVariables = factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
+    }
+
+    [StorableHook(HookType.AfterDeserialization)]
+    private void AfterDeserialization() {
+    }
+
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new LinearRegressionModel(this, cloner);
+    }
+
+    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
+      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
+      double[,] factorData = dataset.ToArray(factorVariables, rows);
+
+      inputData = factorData.HorzCat(inputData);
+
+      int n = inputData.GetLength(0);
+      int columns = inputData.GetLength(1);
+
+      for (int row = 0; row < n; row++) {
+        double p = 0.0;
+        for (int column = 0; column < columns; column++) {
+          p += W[column] * inputData[row, column];
+        }
+        p += W[columns];
+        yield return p;
+      }
+    }
+
+    public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) {
+      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
+      double[,] factorData = dataset.ToArray(factorVariables, rows);
+
+      inputData = factorData.HorzCat(inputData);
+
+      int n = inputData.GetLength(0);
+      int columns = inputData.GetLength(1);
+
+      double[] d = new double[C.GetLength(0)];
+      
+      for (int row = 0; row < n; row++) {
+        for (int column = 0; column < columns; column++) {
+          d[column] = inputData[row,column];
+        }
+        d[columns] = 1;
+
+        double var = 0.0;
+        for(int i=0;i<d.Length;i++) {
+          for(int j = 0;j<d.Length;j++) {
+            var += d[i] * C[i, j] * d[j];
+          }
+        }
+        yield return var + NoiseSigma*NoiseSigma;
+      }
+    }
+
+
+    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
+      return new ConfidenceRegressionSolution(this, new RegressionProblemData(problemData));
+    }
+  }
+}
