[R] Not the same length

peter dalgaard
Tue, 17 Sep 2019 20:32:45 -0000

It depends on what you want to do, which is likely not what you do do....

You might be looking for

y_obs <- ifelse(runif(n) < .9, rnorm(n, y_model, 0.1), rnorm(n, y_model,
0.5))

-pd


> On 17 Sep 2019, at 21:48 , varin sacha via R-help <r-help at
r-project.org> 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)
> 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 at 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 at cbs.dk  Priv: PDalgd at gmail.com