[R] Not the same length

varin sacha v@r|n@@ch@ @end|ng |rom y@hoo@|r
Wed Sep 18 16:47:41 CEST 2019


Dear Peter Dalgaard,

Really appreciated, but my code does not work. There is still a problem ! Here below the reproducible example with 20 variables


library(mgcv) 
library(earth) 
n<-2000 
x<-runif(n, 0, 5) 
z <- rnorm(n, 2, 3) 
a <- runif(n, 0, 5) 
b <- rnorm(n, 2, 3) 
c <- runif(n, 0, 5) 
d <- rnorm(n, 2, 3) 
e <- runif(n, 0, 5) 
f <- rnorm(n, 2, 3) 
g <- runif(n, 0, 5) 
h <- rnorm(n, 2, 3) 
i <-runif(n, 0, 5) 
j <-rnorm(n, 2, 3) 
k <-runif(n, 0, 5) 
l <-rnorm(n, 2, 3) 
m <-runif(n, 0, 5)
 n <-rnorm(n, 2, 3) 
o <-runif(n, 0, 5) 
p <-rnorm(n, 2, 3) 
q <-runif(n, 0, 5) 
r <-rnorm(n, 2, 3)   
y_model<- 0.1*x^3 - 0.5 * z^2 - a^2 + b^2 + 2*c + 3*d - 4*e + 3.5*f - 4.5*g+ 2.5*h + 5.5*i^2 -1.5*j - 6*k + l + 2*m + n + 3*o - 4.5*p + q - r + 10 
y_obs <- y_model +c( rnorm(n*0.97, 0, 0.1), rnorm(n*0.03, 0, 0.5) )   
gam_model<- gam(y_obs~s(x)+s(z)+s(a)+s(b)+s(c)+s(d)+s(e)+s(f)+s(g)+s(h)+s(i)+s(j)+s(k)+s(l)+s(m)+s(n)+s(o)+s(p)+s(q)+s(r)) 
mars_model<-earth(y_obs~x+z+a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r)   
MSE_GAM<-mean((gam_model$fitted.values - y_model)^2) 
MSE_MARS<-mean((mars_model$fitted.values - y_model)^2)   
MSE_GAM 
MSE_MARS






Le mercredi 18 septembre 2019 à 11:04:26 UTC+2, peter dalgaard <pdalgd using gmail.com> a écrit : 





Um, I think not... The mean of the last 200 observation won't line up with the x and z. 

Possibly, if what you want is the last 200 obs to have a different variance, 

y_obs <- y_model + c(rnorm(0.9 * n, 0, 0.1), rnorm(0.1 * n, 0, 0.5))

or

y_obs <- rnorm(n, y_model, rep(c(0.1, 0.5), c(.9 * n, .1 * n)))

-pd


> On 17 Sep 2019, at 22:27 , David Winsemius <dwinsemius using comcast.net> wrote:
> 
> 
> On 9/17/19 12:48 PM, varin sacha via R-help wrote:
>> Dear R-helpers,
>> 
>> Doing dput(x) and dput(y_obs), the 2 vectors are not the same length (1800 for y_obs and 2000 for x)
>> How can I solve the problem ?
>> 
>> Here is the reproducible R code
>> 
>>  #  #  #  #  #  #  #  #  #  #
>> library(mgcv)
>>  library(earth)
>> 
>> n<-2000
>> x<-runif(n, 0, 5)
>>  y_model<- 0.1*x^3 - 0.5 * x^2 - x + 10
>> # y_obs<-rnorm(n*0.9, y_model, 0.1)+rnorm(n*0.1, y_model, 0.5) # maybe not exactly your goal?
> 
> 
> You didn't lay out any goals for analysis, so let me guess what was intended:
> 
> 
> I suspect that you were hoping to model a mixture composed of 90% from one distribution and 10% from another. If I'm right about that guess then you would instead wat to join the samples from each distribution:
> 
> y_obs<-c( rnorm(n*0.9, y_model, 0.1),  rnorm(n*0.1, y_model, 0.5) )
> 
> -- 
> 
> David
> 
> 
>> gam_model<- gam(y_obs~s(x))
>> mars_model<- earth(y_obs~x)
>> MSE_GAM<-mean((gam_model$fitted.values - y_model)^2)
>> MSE_MARS<-mean((mars_model$fitted.values - y_model)^2)
>> MSE_GAM
>> MSE_MARS
>>  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #  #
>> 
>> ______________________________________________
>> 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.
> 
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com


______________________________________________
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.



More information about the R-help mailing list