Thursday, 15 January 2015

Adding a column to a matrix as part of a function in R -



Adding a column to a matrix as part of a function in R -

i brand new r , trying hand @ creating functions first time. trying create function give me regression coefficients, r-square , adjusted r-square value x matrix of values , y vector. had no problem making basic equations, need way create function add together column of ones x matrix user doesn't have set in. in other words, function works great if create first column in matrix c(1), want done function. know there must e extremely simple reply have tried can think of , looked @ many different questions couldn't figure out (because new @ this, lot of info find r online hard me understand). anyways, help appreciated. reply clarification questions best of ability. have been using next sample dataset/r functions test out:

x<-cbind(c(4,6,8,10),c(5,3,6,7)) y<-c(2,6,9,4) fit.lm<-lm(y~x) summary(fit.lm)

if can add together column of ones, function give me same values functions above....my code far:

mylm<-function(y,x) { betahat<-solve(t(x)%*%x)%*%t(x)%*%y yhat<-predict(lm(y~x)) rsquare<-1-((sum((yhat-y)^2))/sum((y-mean(y))^2)) adjrsquare<-1-((sum((yhat-y)^2))/sum((y-mean(y))^2))%*%((length(y)-1)/(length(y)-length(betahat))) results<-list(b=betahat,r2=rsquare,r2a=adjrsquare) return(results) } mylm(y,x)

here's reply doesn't reply problem exactly. assembly required. if doesn't reply question please reformat question:

mylm <- function(x){ cbind(1, x) } mylm(x)

r function matrix

No comments:

Post a Comment