opened
Quicksilver466
username_0
<!--

Welcome!  If you have a question you'd like to ask, you can do it here or on the
mlpack mailing list; see also https://mlpack.org/help.html.

If you're looking for how to get involved and contribute, there's no need to
open an issue---you can see https://www.mlpack.org/community.html instead.

-->

I am getting a couple of errors in the R2 score. Specifically on 3rd last line in Evaluate function. 
So this is the code in which I am getting error 

```
#include<iostream>
#include<mlpack/core.hpp>
#include<mlpack/methods/ann/layer/layer.hpp>
#include<mlpack/methods/ann/ffn.hpp>
#include<mlpack/methods/linear_regression/linear_regression.hpp>
#include<mlpack/core/cv/metrics/r2_score.hpp>

using namespace std;
using namespace mlpack;
/*using namespace mlpack::ann;*/
using namespace mlpack::regression;

int main()
{
	int i;
	int j;
	data::DatasetInfo info;
	info.Type(0)=mlpack::data::Datatype::numeric;
	info.Type(1)=mlpack::data::Datatype::categorical;
	info.Type(2)=mlpack::data::Datatype::numeric;
	info.Type(3)=mlpack::data::Datatype::numeric;
	info.Type(4)=mlpack::data::Datatype::categorical;
	info.Type(5)=mlpack::data::Datatype::categorical;
	info.Type(6)=mlpack::data::Datatype::numeric;
	arma::mat trainData;
	arma::mat traindata;
	data::Load("insurance.csv",trainData,info);
	traindata=trainData.t();
	cout<<"The length of traindata is: "<<size(traindata)<<endl;
	cout<<traindata.row(5)<<endl;
	cout<<traindata.row(5)[2]<<endl;
	arma::mat X=arma::randu(size(traindata)[0],size(traindata)[1]-1);
	arma::mat Y=arma::randu(size(traindata)[0],1);
	for(i=0;i<size(traindata)[0];i++)
	{
		for(j=0;j<size(traindata)[1]-1;j++)
		{
			X.row(i)[j]=traindata.row(i)[j];
		}
	}
	cout<<"The size of X is: "<<size(X)<<endl;
	for(i=0;i<size(traindata)[0];i++)
	{
		Y.row(i)[0]=traindata.row(i)[6];
	}
	cout<<"The size of Y is: "<<size(Y)<<endl;
	X=X.t();
	Y=Y.t();
	LinearRegression lr(X,Y);
	arma::vec parameters = lr.Parameters();
	cout<<"The parameters of linear model are: "<<endl<<parameters<<endl;
	mlpack::cv::R2Score r2;
	cout<<"The r2 score is: "<<r2.Evaluate<LinearRegression,arma::mat,arma::mat>(lr,X,Y)<<endl;
	return 0;
}
```
The errors are

```
In file included from /usr/local/include/mlpack/core/cv/metrics/r2_score.hpp:74,
                 from main.cpp:6:
/usr/local/include/mlpack/core/cv/metrics/r2_score_impl.hpp: In instantiation of ‘static double mlpack::cv::R2Score::Evaluate(MLAlgorithm&, const DataType&, const ResponsesType&) [with MLAlgorithm = mlpack::regression::LinearRegression; DataType = arma::Mat<double>; ResponsesType = arma::Mat<double>]’:
main.cpp:53:85:   required from here
/usr/local/include/mlpack/core/cv/metrics/r2_score_impl.hpp:34:23: error: cannot bind non-const lvalue reference of type ‘arma::rowvec&’ {aka ‘arma::Row<double>&’} to an rvalue of type ‘arma::rowvec’ {aka ‘arma::Row<double>’}
   34 |   model.Predict(data, predictedResponses);
      |                       ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/armadillo:623,
                 from /usr/local/include/mlpack/core/arma_extend/arma_extend.hpp:50,
                 from /usr/local/include/mlpack/prereqs.hpp:120,
                 from /usr/local/include/mlpack/core.hpp:67,
                 from main.cpp:2:
/usr/include/armadillo_bits/Row_meat.hpp:354:1: note:   after user-defined conversion: ‘arma::Row<eT>::Row(const arma::Base<eT, T1>&) [with T1 = arma::Mat<double>; eT = double]’
  354 | Row<eT>::Row(const Base<eT,T1>& X)
      | ^~~~~~~
In file included from main.cpp:5:
/usr/local/include/mlpack/methods/linear_regression/linear_regression.hpp:104:55: note:   initializing argument 2 of ‘void mlpack::regression::LinearRegression::Predict(const mat&, arma::rowvec&) const’
  104 |   void Predict(const arma::mat& points, arma::rowvec& predictions) const;
      |                                         ~~~~~~~~~~~~~~^~~~~~~~~~~
In file included from /usr/local/include/mlpack/core/cv/metrics/r2_score.hpp:74,
                 from main.cpp:6:
/usr/local/include/mlpack/core/cv/metrics/r2_score_impl.hpp:36:36: error: cannot convert ‘arma::enable_if2<true, const arma::Op<arma::Mat<double>, arma::op_mean> >::result’ {aka ‘const arma::Op<arma::Mat<double>, arma::op_mean>’} to ‘double’ in initialization
   36 |   double meanResponses = arma::mean(responses);
      |                          ~~~~~~~~~~^~~~~~~~~~~
      |                                    |
      |                                    arma::enable_if2<true, const arma::Op<arma::Mat<double>, arma::op_mean> >::result {aka const arma::Op<arma::Mat<double>, arma::op_mean>}
```
I am a newbie so I might be wrong but the errors seems to be on the library's side. According to me the first error could solved by adding a const to _arma::rowvec& predictions_ parameter here: 
https://github.com/mlpack/mlpack/blob/287c3bcfd225180c2dd3e0191996dd909a9bf0ee/src/mlpack/methods/linear_regression/linear_regression.hpp#L104

While the second error could be solved by doing _arma::mean(arma::mean(responses))_ instead of _arma::mean(responses)_ here:
https://github.com/mlpack/mlpack/blob/287c3bcfd225180c2dd3e0191996dd909a9bf0ee/src/mlpack/core/cv/metrics/r2_score_impl.hpp#L37
Error while executing R2 score
issue
created
rcurtin
username_1
I think that `ResponsesType` for `R2Score` needs to be set to `arma::rowvec`; so try changing the line in your code

```
	cout<<"The r2 score is: "<<r2.Evaluate<LinearRegression,arma::mat,arma::mat>(lr,X,Y)<<endl;
```

to this:

```
	cout<<"The r2 score is: "<<r2.Evaluate<LinearRegression,arma::mat,arma::rowvec>(lr,X,Y)<<endl;
```

I *think* that should fix the issue you are seeing. :)
comment
created
rcurtin
username_1
Actually, sorry, I took a slightly closer look.  You should also change the type of `Y` to just be an `arma::rowvec` too---it only needs to be a vector, because linear regression responses are one-dimensional.

In fact, the compiler should also be able to infer types correctly for `r2.Evaluate()`.  So once you change the type of `Y`, you could even do `r2.Evaluate(lr, X, Y)` and that should work fine too (because the compiler should select the correct type, `arma::rowvec`, for `Y`).
comment
closed
Quicksilver466
username_0
issue
created
Quicksilver466
username_0
Ah yes that solves it. Thanks a lot! and sorry I completely missed that in the documentation.
comment
created
rcurtin
username_1
If the documentation is unclear and you have suggestions for how it could be improved, please feel free to suggest changes! :)
comment
created
Quicksilver466
username_0
No no that was completely my fault. So no specific suggestions as of now. :+1:
comment