[R] How to create a function for a self created class.

ankit.sethi ankit.sethi at mu-sigma.com
Wed Mar 21 19:35:08 CET 2012


Hi, 
I need to create a method for a class named 'simpleOLS' which I have created
that will compute the coefficients of predictors. 

Here is my code: 

#----------------------------------------------------------------------------------------------------------------------------------------------------------- 

setClass("simpleOLS", representation(dataset="matrix", beta="matrix",
x="matrix", y="matrix",   var="character")) 

computeBetas = function(dataset,str) class('simpleOLS') 
  
setMethod("computeBetas","simpleOLS",betas) 

betas = function(dataset, str) 
{ 
  x<- cbind(int=1,dataset[-which(colnames(dataset) %in% str)]) 
  y<- dataset[,length(dataset)] 
  names(x)<- NULL 
  names(y)<- NULL 
  x.numeric<- sapply(x, as.numeric) #converting into numeric type 
  y.numeric<- sapply(y, as.numeric) #converting into numeric type 
  transpose.x<- t(x.numeric) 
  X<-transpose.x%*%x.numeric 
  inv<- ginv(X, tol=sqrt(.Machine$double.eps)) 
  Y<- inv%*%transpose.x 
  beta<- Y%*%y.numeric 
  return(beta) 
} 

T2<-computeBetas(data.setA1,str) 
#------------------------------------------------------------------------------------------------------------------------------------------------------------ 

But when I call the function it returns me - [1] "character" which is not
the intended output. I am new to object oriented programming in R and any
help will be appreciated. I also want to know if there is any problem with
the way I have created the function. 

Thanks & Regards,

--
View this message in context: http://r.789695.n4.nabble.com/How-to-create-a-function-for-a-self-created-class-tp4493288p4493288.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list