This project is read-only.

How can i use user-defined function?

Jun 30, 2012 at 4:32 AM
Edited Jun 30, 2012 at 9:01 AM

The code:

fn <- function(x, M, V) {
  a <- x[1]
  c <- x[2]
  c(M - a*c/(a-1), V - a*c^2/(a-1)^2/(a-2))
}

but i find i can not call it in R.net even though i run this script successfully. It is returned by parse exception at the line of calling function.

So, how can i use user-defined function?

Thank you so much

P.S.

I have solved it, it is just i made a small mistake in my code

Jul 5, 2012 at 4:10 AM

I have the soame problem,could you give me more information about R.NET customer fucntion? 

Jul 7, 2012 at 8:06 AM
Edited Jul 7, 2012 at 8:07 AM

How to use user-defined function?

Could you give me a example?

Thank you!!

 

Jul 9, 2012 at 7:00 PM
If you need help with R.NET, please include the .NET code you are having trouble with.
If this is a question on how to use R, this is not the right forum. (see https://stat.ethz.ch/mailman/listinfo/r-help )
Jul 11, 2012 at 5:49 AM

I use this method to use user-defined function:

 private  string RF_RES_STATIC = @"res.static<-function(x){
                                            k1 = min(x);
                                            k2 = max(x);
                                            k3 = mean(x);
                                            k4 = sd(x);
                                            k5 = length(x);
                                            k = matrix(nrow=1,ncol=5,
                                            dimnames=list(c('value'),
                                            c('Minimum','Maximum','Mean','Std.Deviation','N')));
                                            k['value','Minimum']=k1;
                                            k['value','Maximum']=k2;
                                            k['value','Mean'] = k3;
                                            k['value','Std.Deviation']=k4;
                                            k['value','N']=k5;
                                            k
                                            }".Replace(" ", "");


 public DataTable ResidualsStatisticsAnalysis()
        {
            DataTable dtResidualsStatistics = new DataTable("dtResidualsStatistics");
            DataColumn[] cols = new DataColumn[] { 
                new DataColumn("typeCol",colStringType),
                new DataColumn("minCol",colDoubleType),
                new DataColumn("maxCol",colDoubleType),
                new DataColumn("meanCol",colDoubleType),
                new DataColumn("sdCol",colDoubleType)
            };
            dtResidualsStatistics.Columns.AddRange(cols);
            engine.Evaluate(RF_RES_STATIC);
            string[] rowsName = new string[] { "Predicted Values", "Residual", "Std. Predicted Value", "Std. Residual"};
            List<string> RESExpressions = new List<string>(){
                   string.Format("res.static({0}$fitted.values)", linearModeName),
                   string.Format("res.static({0}$residuals)", linearModeName),
                   string.Format("res.static(scale({0}$residuals))", linearModeName),
                   string.Format("res.static(scale({0}$residuals))", linearModeName), 
            };
         
            for (int i = 0; i < rowsName.Count(); i++)
            {
                DataRow row = dtResidualsStatistics.NewRow();
                NumericMatrix matrix = engine.Evaluate(RESExpressions[i]).AsNumericMatrix();
                row["typeCol"] = rowsName[i];
                row["minCol"] = matrix["value", "Minimum"];
                row["maxCol"] = matrix["value", "Maximum"];
                row["meanCol"] = matrix["value", "Mean"];
                row["sdCol"] = matrix["value", "Std.Deviation"];
                dtResidualsStatistics.Rows.Add(row);
            }                     
            return dtResidualsStatistics;
        }

It's very import to handle the "punctuation" In R .

 

other excample like this:

 private static string RF_CASE_DIAG = @"case.diag<-function(x){
                                            k=scale(x$residuals);
                                            n=length(k);
                                            m=matrix(ncol=5,dimnames=list(c('value'),c('CaseNum','Std.Residual','Indepnment','PredictedValue','Residual')));
                                            for(i in 1:n){    
                                                if(abs(k[i])>3){
                                                mt=matrix(ncol=5,dimnames=list(c('value'),c('CaseNum','Std.Residual','Indepnment','PredictedValue','Residual')));       
                                                mt['value','CaseNum']=i;
                                                mt['value','Std.Residual'] = k[i];
                                                mt['value','Indepnment'] = x$residuals[i]+x$fitted.values[i];
                                                mt['value','PredictedValue'] = x$fitted.values[i];
                                                mt['value','Residual'] = x$residuals[i];
                                                m=rbind(m,mt);
                                                };
                                            };
                                            m=na.omit(m);
                                            m;
                                        };".Replace("  ", ""); //double blank space for(i in 1:n) problem

Jul 11, 2012 at 5:49 AM
Edited Jul 11, 2012 at 5:51 AM

sorry,twice reply.i can't delete it.

Jul 11, 2012 at 6:53 AM

Dear kulong995,

I am really sorry for replying so late.

As for the problem of user-defined function,

I found the error is caused by my code’s syntax bug.

So, please forget that question.

Best,

Guangwen Liu

From: kulong995 [email removed]
Sent: Wednesday, July 11, 2012 1:50 PM
To: liugw198209@hotmail.com
Subject: Re: How can i use user-defined function? [rdotnet:361462]

From: kulong995

I use this method to use user-defined function:

 private  string RF_RES_STATIC = @"res.static<-function(x){
                                            k1 = min(x);
                                            k2 = max(x);
                                            k3 = mean(x);
                                            k4 = sd(x);
                                            k5 = length(x);
                                            k = matrix(nrow=1,ncol=5,
                                            dimnames=list(c('value'),
                                            c('Minimum','Maximum','Mean','Std.Deviation','N')));
                                            k['value','Minimum']=k1;
                                            k['value','Maximum']=k2;
                                            k['value','Mean'] = k3;
                                            k['value','Std.Deviation']=k4;
                                            k['value','N']=k5;
                                            k
                                            }".Replace(" ", "");
 
 
 public DataTable ResidualsStatisticsAnalysis()
        {
            DataTable dtResidualsStatistics = new DataTable("dtResidualsStatistics");
            DataColumn[] cols = new DataColumn[] { 
                new DataColumn("typeCol",colStringType),
                new DataColumn("minCol",colDoubleType),
                new DataColumn("maxCol",colDoubleType),
                new DataColumn("meanCol",colDoubleType),
                new DataColumn("sdCol",colDoubleType)
            };
            dtResidualsStatistics.Columns.AddRange(cols);
            engine.Evaluate(RF_RES_STATIC);
            string[] rowsName = new string[] { "Predicted Values", "Residual", "Std. Predicted Value", "Std. Residual"};
            List<string> RESExpressions = new List<string>(){
                   string.Format("res.static({0}$fitted.values)", linearModeName),
                   string.Format("res.static({0}$residuals)", linearModeName),
                   string.Format("res.static(scale({0}$residuals))", linearModeName),
                   string.Format("res.static(scale({0}$residuals))", linearModeName), 
            };
         
            for (int i = 0; i < rowsName.Count(); i++)
            {
                DataRow row = dtResidualsStatistics.NewRow();
                NumericMatrix matrix = engine.Evaluate(RESExpressions[i]).AsNumericMatrix();
                row["typeCol"] = rowsName[i];
                row["minCol"] = matrix["value", "Minimum"];
                row["maxCol"] = matrix["value", "Maximum"];
                row["meanCol"] = matrix["value", "Mean"];
                row["sdCol"] = matrix["value", "Std.Deviation"];
                dtResidualsStatistics.Rows.Add(row);
            }                     
            return dtResidualsStatistics;
        }

It's very import to handle the "punctuation" In R .

other excample like this:

 private static string RF_CASE_DIAG = @"case.diag<-function(x){
                                            k=scale(x$residuals);
                                            n=length(k);
                                            m=matrix(ncol=5,dimnames=list(c('value'),c('CaseNum','Std.Residual','Indepnment','PredictedValue','Residual')));
                                            for(i in 1:n){    
                                                if(abs(k[i])>3){
                                                mt=matrix(ncol=5,dimnames=list(c('value'),c('CaseNum','Std.Residual','Indepnment','PredictedValue','Residual')));       
                                                mt['value','CaseNum']=i;
                                                mt['value','Std.Residual'] = k[i];
                                                mt['value','Indepnment'] = x$residuals[i]+x$fitted.values[i];
                                                mt['value','PredictedValue'] = x$fitted.values[i];
                                                mt['value','Residual'] = x$residuals[i];
                                                m=rbind(m,mt);
                                                };
                                            };
                                            m=na.omit(m);
                                            m;
                                        };".Replace("  ", ""); //double blank space for(i in 1:n) problem