Hi all, I´m re-starting (as my name indicates) my little knowlegde of CRAN R. I made this function time before but I don´t know where is the error because nothing appears as an error but the histogram plot doesn´t appear. Should I install some special library to run sapply? pru<-function(){ randz<-matrix(rnorm(200000),100,2000) H<-matrix(0,100,2000) for (j in 2:2000){ for (i in 2:100){ H[1,]<-randz[1,] H[i,j]<-H[i-1,j]+randz[i,j] }} hy<-nrow(H)-1 estima<-H[2:nrow(H),] estima2<-H[1:hy,] a<-estima b<-estima2 mycoef <- function(x, y) coefficients( lm(y ~ x-1) ) rest <- sapply(2:2000, function(i){ y <- a[,i] x <- b[,i] mycoef(x,y) } ) print(summary(rest)) hist(rest,col="blue",breaks=seq(0.6,1.05,0.01),prob=TRUE) lines(density(rest,bw=0.03)) rug(rest) } Many Thanks¡¡¡ [[alternative HTML version deleted]]
I´m using this version of R R version 2.13.2 (2011-09-30) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i386-pc-mingw32/i386 (32-bit) 2012/5/5 Trying To learn again <tryingtolearnagain@gmail.com>> Hi all, > > I´m re-starting (as my name indicates) my little knowlegde of CRAN R. > > I made this function time before but I don´t know where is the error > because nothing appears as an error but the histogram plot doesn´t appear. > Should I install some special library to run sapply? > > > pru<-function(){ > > randz<-matrix(rnorm(200000),100,2000) > > H<-matrix(0,100,2000) > > > for (j in 2:2000){ > for (i in 2:100){ > H[1,]<-randz[1,] > H[i,j]<-H[i-1,j]+randz[i,j] > > > }} > > hy<-nrow(H)-1 > > estima<-H[2:nrow(H),] > > estima2<-H[1:hy,] > > > a<-estima > b<-estima2 > > > mycoef <- function(x, y) coefficients( lm(y ~ x-1) ) > > rest <- sapply(2:2000, function(i){ > y <- a[,i] > x <- b[,i] > mycoef(x,y) > } > ) > > > > print(summary(rest)) > > hist(rest,col="blue",breaks=seq(0.6,1.05,0.01),prob=TRUE) > lines(density(rest,bw=0.03)) > rug(rest) > > > > } > > Many Thanks¡¡¡ > >[[alternative HTML version deleted]]
1. You need to call the function pru() after defining it. 2. Next excercise will be to get rid of all those loops. 3. Next excercide will be to reutn something useful from your function (rather than printing). 4. Upgrade to a recent version of R. Uwe Ligges On 05.05.2012 17:31, Trying To learn again wrote:> I?m using this version of R > > R version 2.13.2 (2011-09-30) > Copyright (C) 2011 The R Foundation for Statistical Computing > ISBN 3-900051-07-0 > Platform: i386-pc-mingw32/i386 (32-bit) > > > 2012/5/5 Trying To learn again<tryingtolearnagain at gmail.com> > >> Hi all, >> >> I?m re-starting (as my name indicates) my little knowlegde of CRAN R. >> >> I made this function time before but I don?t know where is the error >> because nothing appears as an error but the histogram plot doesn?t appear. >> Should I install some special library to run sapply? >> >> >> pru<-function(){ >> >> randz<-matrix(rnorm(200000),100,2000) >> >> H<-matrix(0,100,2000) >> >> >> for (j in 2:2000){ >> for (i in 2:100){ >> H[1,]<-randz[1,] >> H[i,j]<-H[i-1,j]+randz[i,j] >> >> >> }} >> >> hy<-nrow(H)-1 >> >> estima<-H[2:nrow(H),] >> >> estima2<-H[1:hy,] >> >> >> a<-estima >> b<-estima2 >> >> >> mycoef<- function(x, y) coefficients( lm(y ~ x-1) ) >> >> rest<- sapply(2:2000, function(i){ >> y<- a[,i] >> x<- b[,i] >> mycoef(x,y) >> } >> ) >> >> >> >> print(summary(rest)) >> >> hist(rest,col="blue",breaks=seq(0.6,1.05,0.01),prob=TRUE) >> lines(density(rest,bw=0.03)) >> rug(rest) >> >> >> >> } >> >> Many Thanks??? >> >> > > [[alternative HTML version deleted]] > > > > > ______________________________________________ > R-help at r-project.org mailing list > 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.