Skip to content

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

5 messages · varin sacha, Eric Berger, Bert Gunter +1 more

#
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

######################?
#
mean(unlist(lst))


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

            

  
  
#
mean(lst) ### See ?mean. A list cannot be an argument of mean.
lst$mean  ## nonsense! Don't guess -- read the docs.

Here is an an example:
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5
[1] NA
Warning message:
In mean.default(z) : argument is not numeric or logical: returning NA
[1] "list"
[1] 3

Cheers,
Bert




Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

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

            

  
  
#
On 5/8/2018 12:26 PM, varin sacha via R-help wrote:
<<<snip>>>

You need to spend some time with the Introduction to R that came with 
your R installation.  First, lst in your example is not a function, it 
is a list. And as you found, the mean() function does not work on a 
list. Second, your "minimal reproducible" example could have been 
something like this

lst <- list()
for (i in 1:10) lst[i] <- i
mean(lst)  # does not work

The documentation for mean, ?mean, says that it is looking for a numeric 
or logical vector.  To convert your list to a numeric vector you could 
unlist() it.

mean(unlist(lst))


Hope this is helpful,

Dan
#
Many thanks for all of you for your responses.

Best Regards,
SV







Le mardi 8 mai 2018 ? 21:58:37 UTC+2, Daniel Nordlund <djnordlund at gmail.com> a ?crit :
On 5/8/2018 12:26 PM, varin sacha via R-help wrote:
<<<snip>>>

You need to spend some time with the Introduction to R that came with 
your R installation.? First, lst in your example is not a function, it 
is a list. And as you found, the mean() function does not work on a 
list. Second, your "minimal reproducible" example could have been 
something like this


lst <- list()

for (i in 1:10) lst[i] <- i
mean(lst)? # does not work

The documentation for mean, ?mean, says that it is looking for a numeric 
or logical vector.? To convert your list to a numeric vector you could 
unlist() it.

mean(unlist(lst))


Hope this is helpful,

Dan