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 ? #? #? #? #? #? #? #? #? #? #? #? #? #? #? #? #
Dear Varin Sacha, My guess to try to help you is the following: I think you may want to change this: y_obs <- rnorm(n*0.9, y_model, 0.1) + rnorm(n*0.1, y_model, 0.5) for: y_obs <- c( rnorm(n*0.9, y_model, 0.1), rnorm(n*0.1, y_model, 0.5) ) then y_obs:> length(y_obs)[1] 2000 De: varin sacha via R-help Enviado: martes, 17 de septiembre de 2019 21:49 Para: R-help Mailing List Asunto: [R] Not the same length 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. [[alternative HTML version deleted]]
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 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.
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
Many thanks David, it perfectly works. Now, one last think. If I want my R code here below to run let's say B=500 times and at the end I want to get the average for the MSE_GAM and for the MSE_MARS. How can I do that ? library(mgcv) library(earth) n<-2000 x<-runif(n, 0, 5) z <- runif(n, 0, 5) a <- runif(n, 0, 5) ? y_model<- 0.1*x^3 - 0.5 * z^2 - a + 10 y_obs <- c( rnorm(n*0.5, y_model, 0.1), rnorm(n*0.5, y_model, 0.5) ) gam_model<- gam(y_obs~s(x)+s(z)+s(a)) mars_model<-earth(y_obs~x+z+a) ? 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 mardi 17 septembre 2019 ? 22:27:54 UTC+2, David Winsemius <dwinsemius at comcast.net> a ?crit : 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 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.______________________________________________ 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.
Many thanks Ana, it perfectly works. Le mardi 17 septembre 2019 ? 22:12:35 UTC+2, Ana PGG <anacream at gmail.com> a ?crit : Dear Varin Sacha, ? My guess to try to help you is the following: ? I think you may want to change this: y_obs <- rnorm(n*0.9, y_model, 0.1) + rnorm(n*0.1, y_model, 0.5) for: y_obs <- c( rnorm(n*0.9, y_model, 0.1), rnorm(n*0.1, y_model, 0.5) ) then y_obs: ?> length(y_obs)[1] 2000 ? ? De: varin sacha via R-help Enviado: martes, 17 de septiembre de 2019 21:49 Para: R-help Mailing List Asunto: [R] Not the same length ? 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. ?
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 at 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 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. > > ______________________________________________ > 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