Get 3 values not only 1

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Get 3 values not only 1

R help mailing list-2
Dear R-experts,

Here below my R code working but I would like to get 3 values not only 1. The value I get is, according to my R code, the variance value. My goal is to get 3 values : the bias value, the variance value and the MSE value. How to solve my problem ?

Many thanks.

####################
# Data
PIB.hab<-c(12000,34000,25000,43000,12500,32400,76320,45890,76345,90565,76580,45670,23450,34560,65430,65435,56755,87655,90755,45675)
ISQ.2018<-c(564,587,489,421,478,499,521,510,532,476,421,467,539,521,478,532,449,487,465,500)

Dataset=data.frame(ISQ.2018,PIB.hab)

#plot
plot(ISQ.2018,PIB.hab)
plot(ISQ.2018,PIB.hab, main="Droite de régression linéaire", xlab="Score ISQ 2018", ylab="PIB/hab")

#OLS fit
fit1<-lm(PIB.hab~ISQ.2018)
lines(ISQ.2018, fitted(fit1), col="blue", lwd=2)

# Create a list to store the results
lst<-list()

# This statement does the repetitions (looping)

for(i in 1 :1000)
{

n=dim(Dataset)[1]
p=0.667
sam<-sample(1 :n,floor(p*n),replace=FALSE)
Training <-Dataset [sam,]
Testing <- Dataset [-sam,]
fit2<-lm(PIB.hab~ISQ.2018)
ypred<-predict(fit2,newdata=Testing)
y<-Dataset[-sam,]$PIB.hab
MSE <- mean((y-ypred)^2)
biais <- mean(ypred-y)
variance <-mean((ypred- mean(ypred))^2)

lst[i]<-MSE
lst[i]<-biais
lst[i]<-variance

}
mean(unlist(lst))
####################

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Get 3 values not only 1

Jim Lemon-4
Hi varin,
How about this:

Mbv<-data.frame(MSE=rep(NA,1000),
 biais=rep(NA,1000),variance=rep(NA,1000))
for(i in 1 :1000) {
 n<-dim(Dataset)[1]
 p<-0.667
 sam<-sample(1 :n,floor(p*n),replace=FALSE)
 Training <-Dataset [sam,]
Testing <- Dataset [-sam,]
 fit2<-lm(PIB.hab~ISQ.2018)
 ypred<-predict(fit2,newdata=Testing)
 y<-Dataset[-sam,]$PIB.hab
 Mbv$MSE[i] <- mean((y-ypred)^2)
 Mbv$biais[i] <- mean(ypred-y)
 Mbv$variance[i] <-mean((ypred- mean(ypred))^2)
}

You can then do what you wish to the data frame Mbv.

Jim

On Thu, Jan 28, 2021 at 7:37 AM varin sacha via R-help
<[hidden email]> wrote:
>
> Dear R-experts,
>
> Here below my R code working but I would like to get 3 values not only 1. The value I get is, according to my R code, the variance value. My goal is to get 3 values : the bias value, the variance value and the MSE value. How to solve my problem ?
>
> Many thanks.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Get 3 values not only 1

jholtman
In reply to this post by R help mailing list-2
Is this what you are after?  You need to store a vector in the list:

> ####################
> # Data
> PIB.hab<-c(12000,34000,25000,43000,12500,32400,76320,45890,76345,90565,76580,45670,23450,34560,65430,65435,56755,87655,90755,45675)
> ISQ.2018<-c(564,587,489,421,478,499,521,510,532,476,421,467,539,521,478,532,449,487,465,500)
>
> Dataset=data.frame(ISQ.2018,PIB.hab)
>
> #plot
> plot(ISQ.2018,PIB.hab)
> plot(ISQ.2018,PIB.hab, main="Droite de régression linéaire", xlab="Score ISQ 2018", ylab="PIB/hab")
>
> #OLS fit
> fit1<-lm(PIB.hab~ISQ.2018)
> lines(ISQ.2018, fitted(fit1), col="blue", lwd=2)
>
> # Create a list to store the results
> lst<-list()
>
> # This statement does the repetitions (looping)
>
> for(i in 1 :1000)
+ {
+
+   n=dim(Dataset)[1]
+   p=0.667
+   sam<-sample(1 :n,floor(p*n),replace=FALSE)
+   Training <-Dataset [sam,]
+   Testing <- Dataset [-sam,]
+   fit2<-lm(PIB.hab~ISQ.2018)
+   ypred<-predict(fit2,newdata=Testing)
+   y<-Dataset[-sam,]$PIB.hab
+   MSE <- mean((y-ypred)^2)
+   biais <- mean(ypred-y)
+   variance <-mean((ypred- mean(ypred))^2)
+
+   lst[[i]] <- c(MSE = MSE,
+                    biais = biais,
+                    variance = variance)
+   # lst[i]<-MSE
+   # lst[i]<-biais
+   # lst[i]<-variance
+
+ }
>
> # convert to a matrix
>
> x <- as.matrix(do.call(rbind, lst))
> colMeans(x)
           MSE         biais      variance
  5.418175e+08 -4.524548e+01  6.321856e+07
>

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Wed, Jan 27, 2021 at 12:37 PM varin sacha via R-help
<[hidden email]> wrote:

>
> Dear R-experts,
>
> Here below my R code working but I would like to get 3 values not only 1. The value I get is, according to my R code, the variance value. My goal is to get 3 values : the bias value, the variance value and the MSE value. How to solve my problem ?
>
> Many thanks.
>
> ####################
> # Data
> PIB.hab<-c(12000,34000,25000,43000,12500,32400,76320,45890,76345,90565,76580,45670,23450,34560,65430,65435,56755,87655,90755,45675)
> ISQ.2018<-c(564,587,489,421,478,499,521,510,532,476,421,467,539,521,478,532,449,487,465,500)
>
> Dataset=data.frame(ISQ.2018,PIB.hab)
>
> #plot
> plot(ISQ.2018,PIB.hab)
> plot(ISQ.2018,PIB.hab, main="Droite de régression linéaire", xlab="Score ISQ 2018", ylab="PIB/hab")
>
> #OLS fit
> fit1<-lm(PIB.hab~ISQ.2018)
> lines(ISQ.2018, fitted(fit1), col="blue", lwd=2)
>
> # Create a list to store the results
> lst<-list()
>
> # This statement does the repetitions (looping)
>
> for(i in 1 :1000)
> {
>
> n=dim(Dataset)[1]
> p=0.667
> sam<-sample(1 :n,floor(p*n),replace=FALSE)
> Training <-Dataset [sam,]
> Testing <- Dataset [-sam,]
> fit2<-lm(PIB.hab~ISQ.2018)
> ypred<-predict(fit2,newdata=Testing)
> y<-Dataset[-sam,]$PIB.hab
> MSE <- mean((y-ypred)^2)
> biais <- mean(ypred-y)
> variance <-mean((ypred- mean(ypred))^2)
>
> lst[i]<-MSE
> lst[i]<-biais
> lst[i]<-variance
>
> }
> mean(unlist(lst))
> ####################
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.