[R] Average of results coming from B=100 repetitions (looping)

Eric Berger er|cjberger @end|ng |rom gm@||@com
Tue May 8 21:40:24 CEST 2018


mean(unlist(lst))


On Tue, May 8, 2018 at 10:26 PM, varin sacha via R-help <
r-help using r-project.org> wrote:

>
>  Dear R-experts,
>
> Here below the reproducible example. I am trying to get the average of the
> 100 results coming from the "lst" function. I have tried lst$mean and
> mean(lst). It does not work.
> Any help would be highly appreciated.
>
> ####################
>
>  ## R script for getting MedAe and MedAeSQ from HBR model on Testing data
> install.packages("robustbase")
> install.packages( "MASS" )
> install.packages( "quantreg" )
> install.packages( "RobPer")
> install.packages("devtools")
> library("devtools")
> install_github("kloke/hbrfit")
> install.packages('http://www.stat.wmich.edu/mckean/Stat666/
> Pkgs/npsmReg2_0.1.1.tar.gz')
> library(robustbase)
> library(MASS)
> library(quantreg)
> library(RobPer)
> library(hbrfit)
>
> # numeric variables
> A=c(2,3,4,3,2,6,5,6,4,3,5,55,6,5,4,5,6,6,7,52)
> B=c(45,43,23,47,65,21,12,7,18,29,56,45,34,23,12,65,4,34,54,23)
> D=c(21,54,34,12,4,56,74,3,12,71,14,15,63,34,35,23,24,21,69,32)
>
> # Create a dataframe
> BIO<-data.frame(A,B,D)
>
> # Create a list to store the results
> lst<-list()
>
> # This statement does the repetitions (looping)
> for(i in 1 :100)
> {
>
> # randomize sampling seed
> n=dim(BIO)[1]
> p=0.667
>
> # Sample size
> sam=sample(1 :n,floor(p*n),replace=FALSE)
>
> # Sample training data
> Training =BIO [sam,]
>
> # Sample testing data
> Testing = BIO [-sam,]
>
> # Build the HBR model
> HBR<-hbrfit(D ~ A+B)
>
> # Grab the coefficients
> HBR_intercept <- as.numeric(HBR$coefficients[1])
> HBR_coefA <- as.numeric(HBR$coefficients[2])
> HBR_coefB <- as.numeric(HBR$coefficients[3])
>
> # Predict response on testing data
> Testing$pred <- HBR_intercept + HBR_coefA * Testing$A + HBR_coefB
> *Testing$B
>
> # Get errors
> Testing$sq_error <- (Testing$D-Testing$pred)^2
> Testing$abs_error <- abs(Testing$D-Testing$pred)
> MedAe <- median(Testing$abs_error)
> MedAe
> MedAeSQ <-median(Testing$sq_error)
> MedAeSQ
>
> lst[i]<-MedAe
> }
> lst
> mean(lst)
> lst$mean
>
> ######################
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]




More information about the R-help mailing list