Complex R - function/loop -
here challenging yet quiet interesting conflict have... wish this, hope me out here
this "r"
here code running r-squared x1 training
model=randomforest(x1~.,data=training,importance=true,keep.forest=true) predicted=predict(model,newdata=testing[,-1]) actual=testing$x1 rsq=1-sum((actual-predicted)^2)/sum((actual-mean(actual))^2) print(rsq)
here head of training let know how looks like
head(training) x1 x2 x3 x4 x5 x6 68 -3.556526 4.588409 -2.756521 -2.742035 11.542023 -18.405807 23 -1.915947 -0.179710 -0.240580 -0.278259 -0.284058 0.553627 129 -24.252174 -4.869564 4.800001 -14.608688 5.255074 -20.228981 5 -1.637680 -1.147827 -2.005795 -1.121750 0.101440 -1.608688 147 -68.289856 -0.626083 19.933334 -6.637680 15.379715 -11.515945
there x77 1 of these number of rows 73.
my objective make loop of
model=randomforest(x1~.,data=training,importance=true,keep.forest=true) predicted=predict(model,newdata=testing[,-1]) actual=testing$x1 rsq=1-sum((actual-predicted)^2)/sum((actual-mean(actual))^2) print(rsq)
up x77
so to
model=randomforest(x77~.,data=training,importance=true,keep.forest=true) predicted=predict(model,newdata=testing[,-77]) actual=testing$x77 rsq=1-sum((actual-predicted)^2)/sum((actual-mean(actual))^2) print(rsq)
so achieve 77 of r-sqared
my final objective take mean of 77 r-squared
to maxim.k , others
rsq=function(i){ model=randomforest(testing[,1]~.,data=training,importance=true,keep.forest=true) predicted=predict(model,newdata=testing[,-i]) actual=testing[,i] 1-sum((actual-predicted)^2)/sum((actual-mean(actual))^2) } rsq=function(i){ model=randomforest(xi~.,data=training,importance=true,keep.forest=true) predicted=predict(model,newdata=testing[,-i]) actual=testing[,i] 1-sum((actual-predicted)^2)/sum((actual-mean(actual))^2) }
i know second 1 logically makes no sense, need. testing$x1 testing[,1], won't work putting testing[,1] have put in form of "x1"
how that...?
i think maxim.k has alluded already, work
rsq = function(i) { n = colnames(testing)[i] model=randomforest(as.formula(paste(n,"~.")),data=training,importance=true,keep.forest=true) predicted=predict(model,newdata=testing[,-i]) actual=testing[[n]] 1-sum((actual-predicted)^2)/sum((actual-mean(actual))^2) } sapply(1:77, rsq)
Comments
Post a Comment