Re: [R] Summary statistics for matrix columns
HI A.k, I need one more question, if you can answer it please M - matrix(sample(1:8000),nrow=100) colnames(M)- paste(Col,1:ncol(M),sep=) apply(M,2,function(x) c(Min=min(x),1st Qu =quantile(x, 0.25,names=FALSE), Range = range(x), Median = quantile(x, 0.5, names=FALSE), Mean= mean(x),Std=sd(x), 3rd Qu = quantile(x,0.75,names=FALSE), IQR=IQR(x),Max = max(x))) why I get two range . isn't range mean the different between the max and min Thanks Date: Fri, 23 Nov 2012 16:08:12 -0800 From: ml-node+s789695n4650613...@n4.nabble.com To: frespi...@hotmail.com Subject: Re: Summary statistics for matrix columns Hi, No problem. There are a couple of other libraries which deal with summary statistics: library(pastecs) ?stat.desc() # library(matrixStats) #Using the functions from package: matrixStats fun1-function(x){ res-rbind(colMins(x),colQuantiles(x)[,2],colMedians(x),colMeans(x),colSds(x),colQuantiles(x)[,4],colIQRs(x),colMaxs(x)) row.names(res)-c(Min.,1st Qu.,Median,Mean,sd,3rd Qu.,IQR,Max.) res} set.seed(125) x - matrix(sample(1:80),nrow=8) colnames(x)- paste(Col,1:ncol(x),sep=) fun1(x) #Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 #Min.10.0 1.0 17.0 3.0 18.0 11.0 13.0 15.0 #1st Qu. 24.75000 29.5 26.0 7.75000 40.0 17.25000 27.5 34.75000 #Median 34.0 46.0 42.5 35.5 49.5 23.5 51.5 51.5 #Mean42.5 42.75000 41.75000 35.75000 44.87500 26.87500 44.75000 50.12500 #sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 #3rd Qu. 67.75000 58.5 50.0 63.25000 54.25000 30.25000 56.25000 70.5 #IQR 43.0 29.0 24.0 55.5 14.25000 13.0 28.75000 35.75000 #Max.74.0 77.0 76.0 70.0 65.0 63.0 79.0 80.0 # Col9Col10 #Min. 2.0 6.0 #1st Qu. 24.5 12.5 #Median 33.5 48.0 #Mean34.87500 40.75000 #sd 24.39811 28.21727 #3rd Qu. 45.25000 63.0 #IQR 20.75000 50.5 #Max.71.0 72.0 I thought this could be faster than the previous methods. But, it was the slowest. set.seed(125) x1 - matrix(sample(1:80),nrow=1000) colnames(x)- paste(Col,1:ncol(x1),sep=) system.time(fun1(x1)) # user system elapsed # 0.968 0.000 0.956 A.K. From: Fares Said [hidden email] To: arun [hidden email] Cc: Pete Brecknock [hidden email]; R help [hidden email] Sent: Friday, November 23, 2012 10:23 AM Subject: Re: [R] Summary statistics for matrix columns Thank you all Sent from my iPhone On 2012-11-23, at 10:19, arun [hidden email] wrote: HI, You are right. It is slower when compared to Pete's solution: set.seed(125) x - matrix(sample(1:80),nrow=1000) colnames(x)- paste(Col,1:ncol(x),sep=) system.time({ res-sapply(data.frame(x),function(x) c(summary(x),sd=sd(x),IQR=IQR(x))) res1-as.matrix(res) res2-res1[c(1:4,7,5,8,6),] }) # user system elapsed # 0.596 0.000 0.597 system.time({ res-apply(x,2,function(x) c(Min=min(x), 1st Qu =quantile(x, 0.25,names=FALSE), Median = quantile(x, 0.5, names=FALSE), Mean= mean(x), Sd=sd(x), 3rd Qu = quantile(x,0.75,names=FALSE), IQR=IQR(x), Max = max(x))) }) # user system elapsed # 0.384 0.000 0.384 A.K. - Original Message - From: Pete Brecknock [hidden email] To: [hidden email] Cc: Sent: Friday, November 23, 2012 8:42 AM Subject: Re: [R] Summary statistics for matrix columns frespider wrote Hi, it is possible. but don't you think it will slow the code if you convert to data.frame? Thanks Date: Thu, 22 Nov 2012 18:31:35 -0800 From: ml-node+s789695n4650500h51@.nabble To: frespider@ Subject: RE: Summary statistics for matrix columns HI, Is it possible to use as.matrix()? res-sapply(data.frame(x),function(x) c(summary(x),sd=sd(x),IQR=IQR(x))) res1-as.matrix(res) is.matrix(res1) #[1] TRUE res1[c(1:4,7,5,8,6),] #Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 #Min.10.0 1.0 17.0 3.0 18.0 11.0 13.0 15.0 #1st Qu. 24.75000 29.5 26.0 7.75000 40.0 17.25000 27.5 34.75000 #Median 34.0 46.0 42.5 35.5 49.5 23.5 51.5 51.5 #Mean42.5 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 50.12000 #sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 #3rd
Re: [R] Summary statistics for matrix columns
I also don't like to use split function because I have like around 800 columns Date: Thu, 22 Nov 2012 18:08:54 -0800 From: ml-node+s789695n4650496...@n4.nabble.com To: frespi...@hotmail.com Subject: RE: Summary statistics for matrix columns Hi, How about this: res-do.call(cbind,lapply(split(x,col(x)),function(x) c(summary(x),sd=sd(x),IQR=IQR(x colnames(res)-colnames(x) is.matrix(res) [1] TRUE res Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Min.10.0 1.0 17.0 3.0 18.0 11.0 13.0 15.0 1st Qu. 24.75000 29.5 26.0 7.75000 40.0 17.25000 27.5 34.75000 Median 34.0 46.0 42.5 35.5 49.5 23.5 51.5 51.5 Mean42.5 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 50.12000 3rd Qu. 67.75000 58.5 50.0 63.25000 54.25000 30.25000 56.25000 70.5 Max.74.0 77.0 76.0 70.0 65.0 63.0 79.0 80.0 sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 IQR 43.0 29.0 24.0 55.5 14.25000 13.0 28.75000 35.75000 Col9Col10 Min. 2.0 6.0 1st Qu. 24.5 12.5 Median 33.5 48.0 Mean34.88000 40.75000 3rd Qu. 45.25000 63.0 Max.71.0 72.0 sd 24.39811 28.21727 IQR 20.75000 50.5 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650496.html This email was sent by arun kirshna (via Nabble) -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650498.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
[R] Summary statistics for matrix columns
Hi, is there a way I can calculate a summary statistics for a columns matrix let say we have this matrix x - matrix(sample(1:8000),nrow=100) colnames(x)- paste(Col,1:ncol(x),sep=) if I used summary summary(x) i get the output for each column but I need the output to be in matrix with rownames and all the columns beside it this how I want it Col76 Col77 Min. :739 1st Qu. :1846 1630 Median : 3631 3376 Mean: 3804 3617 Sd : 3rd Qu.:5772 5544 IQR: Max. :79527779 Is there an easy way? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Summary statistics for matrix columns
Hi peter, but this doesn't give me them in the order I want. Is there a better approach Thanks -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650492.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Summary statistics for matrix columns
There is still missing some statistics, like sd and IQR and I prefer the output to be matrix Thanks Date: Thu, 22 Nov 2012 18:00:20 -0800 From: ml-node+s789695n4650493...@n4.nabble.com To: frespi...@hotmail.com Subject: Re: Summary statistics for matrix columns HI, You could try this: set.seed(125) x - matrix(sample(1:80),nrow=8) colnames(x)- paste(Col,1:ncol(x),sep=) sapply(data.frame(x),function(x) summary(x)) # Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Col9 Col10 #Min.10.00 1.00 17.00 3.00 18.00 11.00 13.00 15.00 2.00 6.00 #1st Qu. 24.75 29.50 26.00 7.75 40.00 17.25 27.50 34.75 24.50 12.50 #Median 34.00 46.00 42.50 35.50 49.50 23.50 51.50 51.50 33.50 48.00 #Mean42.50 42.75 41.75 35.75 44.88 26.88 44.75 50.12 34.88 40.75 #3rd Qu. 67.75 58.50 50.00 63.25 54.25 30.25 56.25 70.50 45.25 63.00 #Max.74.00 77.00 76.00 70.00 65.00 63.00 79.00 80.00 71.00 72.00 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650493.html To unsubscribe from Summary statistics for matrix columns, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650494.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Summary statistics for matrix columns
HI, but Sd and IQR not in the order I want , Thanks Date: Thu, 22 Nov 2012 18:08:57 -0800 From: ml-node+s789695n4650496...@n4.nabble.com To: frespi...@hotmail.com Subject: RE: Summary statistics for matrix columns Hi, How about this: res-do.call(cbind,lapply(split(x,col(x)),function(x) c(summary(x),sd=sd(x),IQR=IQR(x colnames(res)-colnames(x) is.matrix(res) [1] TRUE res Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 Min.10.0 1.0 17.0 3.0 18.0 11.0 13.0 15.0 1st Qu. 24.75000 29.5 26.0 7.75000 40.0 17.25000 27.5 34.75000 Median 34.0 46.0 42.5 35.5 49.5 23.5 51.5 51.5 Mean42.5 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 50.12000 3rd Qu. 67.75000 58.5 50.0 63.25000 54.25000 30.25000 56.25000 70.5 Max.74.0 77.0 76.0 70.0 65.0 63.0 79.0 80.0 sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 IQR 43.0 29.0 24.0 55.5 14.25000 13.0 28.75000 35.75000 Col9Col10 Min. 2.0 6.0 1st Qu. 24.5 12.5 Median 33.5 48.0 Mean34.88000 40.75000 3rd Qu. 45.25000 63.0 Max.71.0 72.0 sd 24.39811 28.21727 IQR 20.75000 50.5 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650496.html To unsubscribe from Summary statistics for matrix columns, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650497.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Summary statistics for matrix columns
Hi, it is possible. but don't you think it will slow the code if you convert to data.frame? Thanks Date: Thu, 22 Nov 2012 18:31:35 -0800 From: ml-node+s789695n4650500...@n4.nabble.com To: frespi...@hotmail.com Subject: RE: Summary statistics for matrix columns HI, Is it possible to use as.matrix()? res-sapply(data.frame(x),function(x) c(summary(x),sd=sd(x),IQR=IQR(x))) res1-as.matrix(res) is.matrix(res1) #[1] TRUE res1[c(1:4,7,5,8,6),] #Col1 Col2 Col3 Col4 Col5 Col6 Col7 Col8 #Min.10.0 1.0 17.0 3.0 18.0 11.0 13.0 15.0 #1st Qu. 24.75000 29.5 26.0 7.75000 40.0 17.25000 27.5 34.75000 #Median 34.0 46.0 42.5 35.5 49.5 23.5 51.5 51.5 #Mean42.5 42.75000 41.75000 35.75000 44.88000 26.88000 44.75000 50.12000 #sd 25.05993 27.77846 19.57221 28.40397 16.39196 16.60841 21.97239 25.51995 #3rd Qu. 67.75000 58.5 50.0 63.25000 54.25000 30.25000 56.25000 70.5 #IQR 43.0 29.0 24.0 55.5 14.25000 13.0 28.75000 35.75000 #Max.74.0 77.0 76.0 70.0 65.0 63.0 79.0 80.0 # Col9Col10 #Min. 2.0 6.0 #1st Qu. 24.5 12.5 #Median 33.5 48.0 #Mean34.88000 40.75000 #sd 24.39811 28.21727 #3rd Qu. 45.25000 63.0 #IQR 20.75000 50.5 #Max.71.0 72.0 Solves the order and the matrix output! A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650500.html To unsubscribe from Summary statistics for matrix columns, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Summary-statistics-for-matrix-columns-tp4650489p4650501.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
[R] Remove Column from matrix
Hi,, Can I get help with this. I need to remove a column form the matrix if that specific column has his all entry zero, here the code I wrote but it is not working can you help me please namVar - c(TrlgWSST,TrlgWSSE,TrlgWSSR,SSdiff,TrlgWMSE,TrlgWR2,TrlgWR2adj,TrlgSSE,TrlgMSE,TrlgR2,TrlgR2adj,TrSSE,TrMSE,TrR2,TrR2adj,rdf2, TelgSSE,TelgMSE,TelgR2,TelgR2adj,TelgWSSE,TelgWMSE ,TelgWR2,TelgWR2adj,TeSSE,TeMSE,TeR2,TeR2adj,edf2,Runtime) Asse - matrix(0,nrow=5,ncol=length(namVar)) Asse[,1:3]-2 Asse[2,4] -2000 Asse[,5:10]-3 Asse[,11:20]-5 Asse[,21:30]-12 if(all(Asse[,SSdiff]==0)==TRUE){ Asse - Asse[,-which(colnames(Asse)%in%SSdiff)]} -- View this message in context: http://r.789695.n4.nabble.com/Remove-Column-from-matrix-tp4650334.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Remove Column from matrix
Hi, I edited the code sorry, I forgot the line before Can you have look again please? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Remove-Column-from-matrix-tp4650334p4650348.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] R-Square in WLS
Hi Peter, why you are involving -1 with this concept? Can you explain more please Cheers Date: Sun, 18 Nov 2012 23:28:26 -0800 From: ml-node+s789695n4650012...@n4.nabble.com To: frespi...@hotmail.com Subject: Re: R-Square in WLS On Nov 18, 2012, at 21:32 , Thomas Lumley wrote: On Fri, Nov 16, 2012 at 4:48 PM, frespider [hidden email] wrote: Hi, I am fitting a weighted least square regression and trying to compute SSE,SST and SSReg but I am not getting SST = SSReg + SSE and I dont know what I am coding wrong. Can you help please? For a start, you need to replace your mu and muZ by weighted means. The -1 in the model formulas also suggests that there will be problems even in the non-weighted case. The addition formula for SSDs works for successive model reductions, so it is required that the span of the design matrix X contains the vector of all ones. -thomas [snip] ## Y = Log(Z) Scale Yhat - X%*%bhat # predicted values mu - mean(Y) To - Y - mu Er - Y - Yhat Re - Yhat - mu lgSST - sum(Weights*(To)^2)# log SST lgSSE - sum(Weights*(Er)^2)# log SSE lgSSR - sum(Weights*(Re)^2)# log SSR lgR-sq - lgSSR/lgSST ### Z Scale ## Z - exp(Y) muZ - mean(Z) Zhat - exp(Yhat+0.5*Sigma2) ToZ - Z-muZ ErZ - Z - Zhat ReZ - Zhat - muZ SST - sum(Weights*(ToZ)^2) # SST SSE - sum(Weights*(ErZ)^2) # SSE SSR - sum(Weights*(ReZ)^2) # SSR Rsq - SSR/SST I don't understand what is wrong with the code. The sum square regression plus the sum square error do not add up to the sum square total in both the Y scale and Z scale. Y is a normal distribution and Z is log normally distributed. Where is the error? Also, is there a way to calculate the weighted sum square? -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland [[alternative HTML version deleted]] __ [hidden email] 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. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: [hidden email] Priv: [hidden email] __ [hidden email] 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. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/R-Square-in-WLS-tp4649693p4650012.html To unsubscribe from R-Square in WLS, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/R-Square-in-WLS-tp4649693p4650032.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] R-Square in WLS
Hi Thomas, Can you please edit my code in way it works. I appericated your help. I need to calculate the R2 in the Z scale and it is not make sense to me. or Can I have some documentation to read? Thanks Date: Sun, 18 Nov 2012 12:35:03 -0800 From: ml-node+s789695n4649969...@n4.nabble.com To: frespi...@hotmail.com Subject: Re: R-Square in WLS On Fri, Nov 16, 2012 at 4:48 PM, frespider [hidden email] wrote: Hi, I am fitting a weighted least square regression and trying to compute SSE,SST and SSReg but I am not getting SST = SSReg + SSE and I dont know what I am coding wrong. Can you help please? For a start, you need to replace your mu and muZ by weighted means. -thomas [snip] ## Y = Log(Z) Scale Yhat - X%*%bhat # predicted values mu - mean(Y) To - Y - mu Er - Y - Yhat Re - Yhat - mu lgSST - sum(Weights*(To)^2)# log SST lgSSE - sum(Weights*(Er)^2)# log SSE lgSSR - sum(Weights*(Re)^2)# log SSR lgR-sq - lgSSR/lgSST ### Z Scale ## Z - exp(Y) muZ - mean(Z) Zhat - exp(Yhat+0.5*Sigma2) ToZ - Z-muZ ErZ - Z - Zhat ReZ - Zhat - muZ SST - sum(Weights*(ToZ)^2) # SST SSE - sum(Weights*(ErZ)^2) # SSE SSR - sum(Weights*(ReZ)^2) # SSR Rsq - SSR/SST I don't understand what is wrong with the code. The sum square regression plus the sum square error do not add up to the sum square total in both the Y scale and Z scale. Y is a normal distribution and Z is log normally distributed. Where is the error? Also, is there a way to calculate the weighted sum square? -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland [[alternative HTML version deleted]] __ [hidden email] 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. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/R-Square-in-WLS-tp4649693p4649969.html To unsubscribe from R-Square in WLS, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/R-Square-in-WLS-tp4649693p4649983.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
[R] Multiple Vector with matrix in R
Hi Can someone show me an easy way to multiple a weighted vector with an matrix? example below mat1-matrix(sample(1:100,80,replace=TRUE),ncol=8) w - 1/1:10 I want the first element in w to be multiplied by the first row of mat1 and 2nd element in w to be multiplied with the 2nd row and so on. I have huge matrix is there an easy way other than diag(w)%*%mat1 Thanks -- View this message in context: http://r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Multiple Vector with matrix in R
Hi A.K Here is the error I get when I use %*% dim(X) [1] 71142 219 length(Weights) [1] 71142 Wx-diag(Weights)%*%X Error in array(0, c(n, p)) : 'dim' specifies too large an array That is why I asked for different and faster method -- View this message in context: http://r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764p4649774.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Multiple Vector with matrix in R
Hi No i didn't get error it executed too fast, I had question about the Sum squares in the weighted least square if u can help me I would I appreciated Thanks Date: Fri, 16 Nov 2012 09:46:59 -0800 From: ml-node+s789695n4649775...@n4.nabble.com To: frespi...@hotmail.com Subject: Re: Multiple Vector with matrix in R HI, Did you got the same error with sweep()? A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764p4649775.html To unsubscribe from Multiple Vector with matrix in R, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Multiple-Vector-with-matrix-in-R-tp4649764p4649776.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
[R] R-Square in WLS
Hi, I am fitting a weighted least square regression and trying to compute SSE,SST and SSReg but I am not getting SST = SSReg + SSE and I dont know what I am coding wrong. Can you help please? xnam -colnames(X) # colnames Design Matrix fmla1 - as.formula(paste(Y ~,paste(xnam, collapse= +),-1,sep=)) fitlm - lm(formula=fmla1,data = data.frame(cbind(X,Y))) ResiSqr - (residuals(fitlm))*(residuals(fitlm)) fmla2 - as.formula(paste(ResiSqr ~ , paste(xnam, collapse= +),-1,sep=)) fitResi - lm(formula=fmla2,data = data.frame(cbind(x,ResiSqr))) # This fit is to calculate the weights bResi - coef(fitResi) Sigma2 - X%*%bResi # Var(Y) Weights - 1/Sigma2 for(i in 1:length(Weights)){ Weights[which(ifelse(Weights=0.5,TRUE,FALSE))] - 0.5 Weights[which(ifelse(Weights=200,TRUE,FALSE))] - 200 Sigma2[which(ifelse(Sigma2=0,TRUE,FALSE))] - 0# set -ve Var(Y) to zero #+++ Fit WLS +++ fitwls - lm(formula = fmla1,weights = Weights,data = data.frame(cbind(X,Y))) bhat - coef(fitwls) ## Y = Log(Z) Scale Yhat - X%*%bhat # predicted values mu - mean(Y) To - Y - mu Er - Y - Yhat Re - Yhat - mu lgSST - sum(Weights*(To)^2)# log SST lgSSE - sum(Weights*(Er)^2)# log SSE lgSSR - sum(Weights*(Re)^2)# log SSR lgR-sq - lgSSR/lgSST ### Z Scale ## Z - exp(Y) muZ - mean(Z) Zhat - exp(Yhat+0.5*Sigma2) ToZ - Z-muZ ErZ - Z - Zhat ReZ - Zhat - muZ SST - sum(Weights*(ToZ)^2) # SST SSE - sum(Weights*(ErZ)^2) # SSE SSR - sum(Weights*(ReZ)^2) # SSR Rsq - SSR/SST I don't understand what is wrong with the code. The sum square regression plus the sum square error do not add up to the sum square total in both the Y scale and Z scale. Y is a normal distribution and Z is log normally distributed. Where is the error? Also, is there a way to calculate the weighted sum square? -- View this message in context: http://r.789695.n4.nabble.com/R-Square-in-WLS-tp4649693.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Calling R object from R function
Hi, I edit my post, Can you please help with this matter? Hi, Can you please help me with this please? What I am trying to do is call a vector from R function and used in the new function # Initialize some data Dat - cbind( a0 = rep(1, 40), a = rep(0:1, 20), b = rep(c(1,0), each = 20), c0=c(rep(0,12),rep(1,28)), c1=c(rep(1,5),rep(0,35)), c2=c(rep(1,8),rep(0,32)), c3=c(rep(1,23),rep(0,17)), c4=c(rep(1,6),rep(0,34)), Y = rnorm(40,2,30)) colnames(Dat) - c(a0,a,b,c0,c1,c2,c3,c4,c5,Y) M1 - function(Trdat,Tedat,mdat,nsam,conv){ vectx - c(1,4,6,7,9) vectz - c(2,3,7,1,4,9) X - Trdat[,vectx] Z - Trdat[,vectz] Y - Trdat[,ncol(Trdat)] TesX - Tedat[,vectx] TesZ - Tedat[,vectz] TesY - Tedat[,ncol(Tedat)] Treig - eigen(crossprod(X))$values if(any(abs(Treig) conv)) stop(In M1 the design matrix (X) is singular for simulation , nsam) Comp - c(nCol(X)= ncol(X),nCol(Z)= ncol(Z),Is length(Y)=nrow(X)= length(Y)==nrow(X), Is length(Y)=nrow(Z)= length(Y)==nrow(Z)) list(vectx = vectx, vectz = vectz, X = X, Z = Z, Y = Y, TesX = TesX, TesZ = TesZ, TesY = TesY, Comp = Comp) } get.m - function(dat,asim,ModelFun,M,conv){ Sim - list() modInd - ModelFun(Trdat=dat,Tedat=dat,mdat=dat,nsam=-1,conv=conv) # HERE WHERE I NEED HELP i only need to import vectx and vectz that is why I set Trdat=Tedat=dat if(M==1){ vecx - modInd$vectx vecz - modInd$vectz px - length(vecx) pz - length(vecz) pk - length(modInd$Comp) nam -colnames(dat[,vecx]) Asse - matrix(NA,nrow=asim,ncol=px) Check - matrix(NA,nrow=pk,ncol=asim) colnames(Check) - paste(CheckIter,1:asim,sep=) } else { vecx - modInd$vectx vecz - modInd$vectz px - length(vecx) pz - length(vecz) pk - length(modInd$Comp) nam -colnames(dat[,vecx]) Asse - matrix(NA,nrow=asim,ncol=px) Check - matrix(NA,nrow=pk,ncol=asim) colnames(Check) - paste(CheckIter,1:asim,sep=) } for(k in 1:asim){ cat(Iter #,paste(k),\n) #== # Start Sampling code #== # Sample the Index for Train Set set.seed(k) Indx-sample(1:nrow(dat),nrow(dat),replace=T) SamDat - dat[Indx,] # Split Data set.seed(k) TrainInd - sample(1:nrow(SamDat), trunc(2*length(1:nrow(SamDat))/3)) # Sample 2/3 of the data TrSet - SamDat[TrainInd,] # Train data Hold 1/3 of the data TeSet - SamDat[-TrainInd,] # hold 1/3 of the data Trind - ceiling((2*length(Indx))/3) Model - ModelFun(Trdat=TrSet,Tedat=TeSet,mdat=dat,nsam=k,conv=conv) Y - Model$Y X - Model$X Z - Model$Z TesX - Model$TesX TesZ - Model$TesZ TesY - Model$TesY xnam -colnames(X) znam -colnames(Z) pc - ncol(X) fmla - as.formula(paste(Y ~,paste(xnam, collapse= +),-1,sep=)) fitlm - lm(formula=fmla,data = data.frame(cbind(X,Y))) ResiSqr - (residuals(fitlm))*(residuals(fitlm)) Check[,k] - Model$Comp Asse[k,1:pc] - coef(fitlm) } Sim$Check - Check Sim$Asse - Asse return(Asse) } get.m(dat=Dat,asim=6,ModelFun=M1,M=1,conv=1e-4) -- View this message in context: http://r.789695.n4.nabble.com/Calling-R-object-from-R-function-tp4648714p4648919.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Calling R object from R function
Hi, Can you please help me with this please? What I am trying to do is call a vector from R function and used in the new function So I create 4 functions with these arguments M11 - function(TrainData,TestData,mdat,nsam) { ls - list() I have few statments one of them is vectx - c(,1,2,3,4,5,6,6) vectz - c(12,34,5,6,78,9,90) and then ls(vectx=vtecx,vectz=vectz) return(ls) } Then I great an new function has the following arguments get.m - function(dat,asim,ModelFun,M){ Sim - list() modInd - ModelFun(TrainData=dat,TestData=dat,mdat=dat,nsam=0.1) *# HERE IS MY PROBLEM HOW CAN I JUST CALL vectx without fitting the ModelFun becasue as you see in the bottom TrainData and Test are arguments used after resampling. I dont want to use the main dat argument?* if(M==11){ vecx - modInd$vectx px - length(vecx) # ncol for X nam -colnames(dat[,vecx]) Asse - matrix(NA,nrow=asim,ncol=px) colnames(Asse) - nam } for(i in 1:asim){ set.seed(i) Samdat - dat[sample(1:nrow(dat), nrow(dat), replace = TRUE), ] TrSet - Samdat[1:50,] TeSet - Samdat[51:75,] } Model- ModelFun(TrainData=TrSet ,TestData=TeSet ,mdat=dat,nsam=i) Y - Model$Y X - Model$X fit - lm(Y~X) return(fit) } -- View this message in context: http://r.789695.n4.nabble.com/Calling-R-object-from-R-function-tp4648714.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] GLM and WLS
Hi I am running some simulation in R after resampling from a huge data set 500 columns and 86759 rows and fit these two model and calculate R-square fit - lm(Y~X,weights = Weights) bhat - coef(fit) Yhat - X%*%bhat SST - sum((Y - mean(Y))^2) SSE - sum((Y - Yhat)^2) MSE - SSE/(n-pc) aRsqr - 1-(((n-1)*SSE)/((n-pc)*SST)) and the GLM Model fitGLM - glm(Y~X,family=Gamma(link = log)) Yhat -exp( X%*%bhat ) SST - sum((Y - mean(Y))^2) SSE - sum((Y - Yhat)^2) MSE - SSE/(n-pc) aRsqr - 1-(((n-1)*SSE)/((n-pc)*SST)) I am getting a negative R-square which I don't understand why? Can some one let me know why and if I can investigate that how I do that. Thanks -- View this message in context: http://r.789695.n4.nabble.com/GLM-and-WLS-tp4648531.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] Saving R Graph to a file
Hi I am not sure why I can't get my plot saved to a file as .ps, I searched online and I found that I have to use something is called postscript,png or pdf function which I did but still not working. Actually what I have is a matrix with almost 300-400 columns. I need to create a histogram and boxplot for some columns as .ps file (with reasonable size if i can adjust that would be nice also) so I can import them in my latex code to display a good chart on my report. And I found out R display a certain limit of device. Can you please help me code this? This an example I create data(CO2) png(filename=C:/R/figure.png, height=295, width=300, bg=white) hist(CO2[,4]) device.off() pdf(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) postscript(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) Thanks -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Saving R Graph to a file
Hi guys, I really appreciated all your responds, I made mistake on my example below hwere I wrote device.off() where it should be dev.off(). I read all the help documnet for odf png and postscript before I posted my question but it didn't help. Can some one please code an example? Thanks Date: Sun, 4 Nov 2012 02:39:13 -0800 From: ml-node+s789695n4648380...@n4.nabble.com To: frespi...@hotmail.com Subject: Re: Saving R Graph to a file On 11/4/2012 4:32 AM, Robert Baer wrote: Some hints: For pdf(), height and width are in inches, not pixels. dev.off() is necessary after drawing the image for pdf(). The name for the file argument (file=c:/figure.xxx) is file not filename hist(CO2[,5]) is more interesting And yes, ?pdf ?postscript ?png On 11/3/2012 11:16 PM, frespider wrote: Hi I am not sure why I can't get my plot saved to a file as .ps, I searched online and I found that I have to use something is called postscript,png or pdf function which I did but still not working. Actually what I have is a matrix with almost 300-400 columns. I need to create a histogram and boxplot for some columns as .ps file (with reasonable size if i can adjust that would be nice also) so I can import them in my latex code to display a good chart on my report. And I found out R display a certain limit of device. Can you please help me code this? This an example I create data(CO2) png(filename=C:/R/figure.png, height=295, width=300, bg=white) hist(CO2[,4]) device.off() pdf(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) postscript(filename=C:/R/figure.pdf, height=295, width=300, bg=white) hist(CO2[,4]) Thanks -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email] 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. -- __ Robert W Baer, Ph.D. Professor of Physiology Kirksville College of Osteopathic Medicine A. T. Still University of Health Sciences Kirksville, MO 63501 US __ [hidden email] 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. If you reply to this email, your message will be added to the discussion below:http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369p4648380.html To unsubscribe from Saving R Graph to a file, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Saving-R-Graph-to-a-file-tp4648369p4648393.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.
[R] Return Vector Component
Hi, I am new R user and I am still learn this fabulous software. I am stuck on this part. I need to check 2 character vectors v1 - c(age_1,age_2,age_3,age_4,age_5,age_6,height_1,height_2,height_3,height_4,height_5,height_6,height_7,height_8) and v2 - c(sex_1,sex_2,sex_3,age_height_1_1,age_height_2_2,age_height_3_3,age_height_4_4,age_height_5_5,age_height_6_6, age_height_1_7,age_height_2_8) what I really want to do is loop over v2 and check if its interaction component have any element in v1then return this component (i.e. v2 [4] = age_height_1_1 then check v1 which has age_1 or height_1 then return age_height_1_1) . Is there a way to do that? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Return-Vector-Component-tp4648115.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Return Vector Component
Hi A.K. Thank you so much for replying this could work but the problem you recreate the newv1, to match v2 but I don't want to do that. I just want to check if v2 has that component from v1 THanks Date: Thu, 1 Nov 2012 06:32:03 -0700 From: ml-node+s789695n4648123...@n4.nabble.com To: frespi...@hotmail.com Subject: Re: Return Vector Component Hi, Not sure whether I understand it correctly. v1 - c(age_1,age_2,age_3,age_4,age_5,age_6,height_1,height_2,height_3,height_4,height_5,height_6,height_7,height_8) v2 - c(sex_1,sex_2,sex_3,age_height_1_1,age_height_2_2,age_height_3_3,age_height_4_4,age_height_5_5,age_height_6_6, age_height_1_7,age_height_2_8) v3-gsub((age{0,1}\\_).*,\\1,v1) v4-gsub(age{0,1}\\_(.*),\\1,v1) v5-gsub((height{0,1}\\_).*,\\1,v1) v6-gsub(height{0,1}\\_(.*),\\1,v1) newv1-paste0(v3[grep(age,v3)],v5[grep(height,v5)],v4[!v4%in%v4[grep(height,v4)]],_,v6[!v6%in%v6[grep(age,v6)]]) newv1[newv1%in%v2] #[1] age_height_1_1 age_height_2_2 age_height_3_3 age_height_4_4 #[5] age_height_5_5 age_height_6_6 age_height_1_7 age_height_2_8 A.K. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Return-Vector-Component-tp4648115p4648123.html To unsubscribe from Return Vector Component, click here. NAML -- View this message in context: http://r.789695.n4.nabble.com/Return-Vector-Component-tp4648115p4648126.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]] __ R-help@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.