[R] reverse string
Dear all, Are there any functions in R to reverse the order of the string. smth like reverse(abc) to get cba? Thanks a lot. [[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] Calculation optimization advice needed
Dear all, I produced a part of code which does what needed but by estimations I would need to wait for that for 300 hours!!! Maybe someone can give a me a glance advice or something. Thanks a lot! I need to define maximum adjusted R-squares from regression model: returns as dependent variable( here simulated to be normal) and 3 factors out of say 14 independent (simulated here). I need to run lm model for all possible combinations of factors and this way find the max adj R-squared. This should be done 100 times. Below I provide my code for that. It takes around 3 minutes to calculate, while I need to repeat the whole procedure for 6000 stocks! library(Combinations) # 14 factors with returns normally distributed factors.r - matrix(data = rnorm(200), nrow = 200, ncol = 14) # simulated normal returns (nrows equals to #of simulations, ncol length of history) simul - matrix(data = rnorm(200), nrow = 100, ncol = 200) # possible combinations of 3 out of 14 factors # 364 different combinations fact - combinations(14, 3) # Elements of matrix are whole vectors of factors' returns each row corresponds to the combination of factors from matrix factors AAA - matrix(as.list(factors.r[, t(fact)]), nrow = 364, ncol = 3) # Function to calculate R-squared for the linear regression model AdjRsqrFunc - function(x, a){ summary(lm(a ~ x[[1]] + x[[2]] + x[[3]]))$adj.r.squared } max.seq - NA # I need to check all the combinations of factors and find the model which brings the highest adjusted R-squared # for every simulation out of 100 for (i in 1:100){ xyz - apply(AAA, 1, AdjRsqrFunc, a = simul[i, ]) max.seq - c(maxrsq.seq, max(xyz)) } [[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] lm prediction strange error
Dear all, I have an error in the simple prediction function for lm(). Maybe someone experienced the same? xma - matrix(data = 0, nrow = 100, ncol = 2) xma[, 1] - rnorm(100) xma[, 2] - rchisq(100, df = 3) m1 - lm(xma[, 1] ~ xma[, 2]) predict(m1, as.data.frame(seq(-13, 13, 0.5))) Thanks a lot, Trafim [[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] lm prediction strange error
Thanks a lot, Dimitris! I see - data should be defined precisely as data.frame. On Mon, Aug 16, 2010 at 2:47 PM, Dimitris Rizopoulos d.rizopou...@erasmusmc.nl wrote: better try it this way: DF - data.frame(y = rnorm(100), x = rchisq(100, df = 3)) m - lm(y ~ x, DF) predict(m, data.frame(x = seq(-13, 13, 0.5))) I hope it helps. Best, Dimitris On 8/16/2010 2:37 PM, Trafim Vanishek wrote: Dear all, I have an error in the simple prediction function for lm(). Maybe someone experienced the same? xma- matrix(data = 0, nrow = 100, ncol = 2) xma[, 1]- rnorm(100) xma[, 2]- rchisq(100, df = 3) m1- lm(xma[, 1] ~ xma[, 2]) predict(m1, as.data.frame(seq(-13, 13, 0.5))) Thanks a lot, Trafim [[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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 [[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] minor diagonal in R
Dear all, seems that easy question but cannot find the function for that. How to get the elements of the minor diagonal of the matrix? Thanks a lot. [[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] createDataPartition
Dear all, does anyone know how to define the structure of the required samples using function createDataPartition, meaning proportions of different types of variable in the partition? Smth like this for iris data: createDataPartition(y = c(setosa = .5, virginica = .3, versicolor = .2), times = 10, p = .7, list = FALSE) Thanks a lot for your help. Regards, Trafim [[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] Joint density kde2d works improperly?
Dear Dennis, thanks a lot for your response. I have two time series and need to approximate there joint density. The only thing I cannot find out is how to find the values of this function in the points not from the resulting grid matrix Thanks a lot You're using a function that provides an estimate of a *continuous* bivariate density to approximate a bivariate discrete distribution? If your joint distribution is discrete, there are better ways to visualize it, and I'll leave it up to you to discover how. (Hint: look at the 3D graphics packages.) DM On Wed, Dec 2, 2009 at 2:14 AM, Trafim rdapam...@gmail.com wrote: Dear all, Please, look at the following code: attach(geyser) f1 - kde2d(duration, waiting, n = 5) a - 0 for (i in 1:5){ for (j in 1:5){ a - a + f1$z[i,j] } } As far as I understood from Help kde2d returns matrix elements of which are values of joint probability mass function Pr(X=x,Y=y) therefore, sum of its elements should sum to 1. Which is not the case from my check. Where is the problem here? Thanks a lot. [[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. [[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] How build hist combining data
I would like to ask if there is some simple way for building a hist from the following data. mm -c(2,3,0,4,5,0,2,9,0) mmm - matrix(mm,ncol=3, nrow=3) x - c(0,1.45,2.9) in mmm there are frequencies (each column is separate histogram and each row corresponds to value of x). So that for the first histogram we have 2 times interval [0,1.45] and 3 times [1.45,2.90]. Thanks a lot! [[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] Bootstrapping in R
Dear all, I have some error trying to bootstrap from a matrix. The error message is Error in sample(n, n * R, replace = TRUE) : element 2 is empty; the part of the args list of '*' being evaluated was: (n, R) vv - c(0.5,3.2,5.4,1.1,1.4,1.2,2.3,2.0) Reg - matrix(data=vv, nrow = 4, ncol = 2) bootcoeff - function(x){ coefficients(lm(x[,1]~x[,2]))[2]+1 } boot(Reg, bootcoeff) It is just an example, in reality I have a matrix in rows of which I have x and y for which I need to make a regression to find the slope coeff bootstrapping from rows. Thanks a lot for the help. [[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] Bootstrapping in R
I missed number of bootstrap replicates R boot(Reg, bootcoeff, R=10) but still it doesn't work Error in statistic(data, original, ...) : unused argument(s) (original) On Wed, Dec 9, 2009 at 2:46 PM, Trafim Vanishek rdapam...@gmail.com wrote: Dear all, I have some error trying to bootstrap from a matrix. The error message is Error in sample(n, n * R, replace = TRUE) : element 2 is empty; the part of the args list of '*' being evaluated was: (n, R) vv - c(0.5,3.2,5.4,1.1,1.4,1.2,2.3,2.0) Reg - matrix(data=vv, nrow = 4, ncol = 2) bootcoeff - function(x){ coefficients(lm(x[,1]~x[,2]))[2]+1 } boot(Reg, bootcoeff) It is just an example, in reality I have a matrix in rows of which I have x and y for which I need to make a regression to find the slope coeff bootstrapping from rows. Thanks a lot for the help. [[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] Printing to PDF in for
Hi everybody, I would like to ask if it is possible using pdf function or some other to print plots in cycle such that every new plot is on new page. like this pdf(file=D:/Plot.pdf,width = 9.25,height=9.25, family=Helvetica,pointsize=8,bg=white) for (i in 1:10){ x - seq(1,40,1) y - 2*x+1+5*rnorm(length(x)) hist(y,freq = FALSE) plot(density(y)) } dev.off( ) but the problem is that I have a lot of other code in the cycle which I don't want to be printed in PDF. Thanks a lot for the help [[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] Extract p-value from linear model
Dear all, I have the following question. Is it possible in R to call for the p-value directly in a model like this a - c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) b - c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) summary(lm(a~(b+0))) or even in this a - c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) b - c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) summary(lm(a~b)) to call the p-values directly for every estimated parameter. Thanks a lot for the help! [[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] Graph titles from massive
Dear all, I would like to ask you if there is a possibility in R to give the names to graphs which are not const. For example, How to name each plot, or to add notes like a=x[i], b=y[i] in this cycle x-c(1,2,3,4,5,6) y-c(7,8,9,10,11,12) par(mfrow=c(2,3)) for (i in 1:6){ plot(x,y) } Thank you for your time and help! [[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] Fitting chi-squared distribution
Dear all, I would like to ask if there is a simple was in R to fit the chi-squared distribution to the empirical data? Thanks a lot! [[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] plot prices and dates in a nice way
Dear all, I currently experience the problem with nicely plotting price data against the dates. Data - read.csv(C:/IBM.csv, header = TRUE, sep = ,) plot(Data[,1], Data[,2]) I cannot find the way how can I choose the # of breaks for the x axis - dates in this case? Thanks a lot __ 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] strange floor rounding
Dear all, might seem and easy question but I cannot figure it out. floor(100*(.58)) [1] 57 where is the trick here? And how can I end up with the right answer? Thanks a lot everybody for your help. Trafim [[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] strange floor rounding
Thanks everybody for referring me to FAQ 7.31 but I don't see how to solve it. I am giving a concrete number and I need to get 58 not 57. Seems, there is no way? On Fri, Jul 9, 2010 at 4:05 PM, David Winsemius dwinsem...@comcast.netwrote: On Jul 9, 2010, at 9:46 AM, Trafim Vanishek wrote: Dear all, might seem and easy question but I cannot figure it out. floor(100*(.58)) [1] 57 where is the trick here? FAQ 7.31 And how can I end up with the right answer? Define right, please. (There have been several questions in the last week for which FAQ 7.31 was the answer and some of the responses had useful links.) -- David Winsemius, MD West Hartford, CT [[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] = returns wrong result? Why
Dear all, Does anybody know the probable reason why = gives false when it should give true? These two variables are of the same type, and everything works in the cycle but then it stops when they are equal. this is the output result Rk[47] = RB[21] [1] FALSE Rk[47] [1] 0.002842007 RB[21] [1] 0.002842007 Thanks a lot. [[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] plot type any symbols?
Dear all, I have a question is there a possibility to plot points with different symbols like stars, crosses? I looked at different types for plot command and didn't find anything like that. Thanks a lot. [[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] Extract R-squared from summary of lm
Dear all, I cannot find to explicitly get the R-squared or adjusted R-squared from summary(lm()) Thanks a lot! [[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] GARCH (1,1) negative volatility???
Dear all, I am using GARCH (1,1) model to simulate volatility. But seems that I am missing something about how it works in R. The following code produces negative results, though vola cannot be. What is wrong here? library(fSeries) library(fGarch) spec = garchSpec(model = list(omega = 0.01, alpha = 0.13, beta = 0.86)) gat - garchSim(spec, n = 10) Thanks a lot! [[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] Plot - specification for grid of x axis
Dear all, I have a simple question for which I cannot find the answer. I need to make an easy plot, but for the x axis I need to be able to specify by myself the division of x axis from x[,1] either every single observation, or every 5th, or 10th or 20th x - matrix(data=NA, nrow=100, ncol=2) x[,1]-seq(1,100,1) x[,2]-rnorm(100) Thanks a lot for your help!!! [[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] diagnostic plots
Dear all, does anybody ever encountered the problem with diagnostic plots? x-rnorm(100) y-rnorm(100) plot(lm(x~y)) It gives the following message Waiting to confirm page change... and nothing happens. Thanks a lot! [[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] diagnostic plots
Thanks a lot everybody! I see how it works now. But now the other question: I would like to see the Cook's distance plot which is more understandable for me not the Residuals vs Leverage. The one it shows in the examples http://www.personality-project.org/r/r.lm.html But for some reason it doesn't show up. Thanks a lot. [[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] Extract p-value from lm for the whole model
Dear all, I would like to ask how to extract the p-value for the whole model from summary(lm). This didn't help a lot summary.lm summary(lm(speed~dist, cars)) Thanks a lot! [[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] Integral of function of dnorm
Dear all, How is it possible in R to calculate the following integral: Integral(-Inf, Inf)[log(dnorm(mean = 3, sd = 1))] how can I define that the density dnorm is taken on (-Inf, Inf) Thanks a lot! [[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] Integral of function of dnorm
This is exactly what I mean. I need to find integrate(dnorm(mean=8,sd=1)*log(dnorm(mean=8,sd=1)), -Inf, Inf) Which doesn't work like that, because it says: Error in dnorm(mean = 8, sd = 1) : element 1 is empty; the part of the args list of '.Internal' being evaluated was: (x, mean, sd, log) So how can I define x? THanks a lot Dear all, How is it possible in R to calculate the following integral: Integral(-Inf, Inf)[log(dnorm(mean = 3, sd = 1))] how can I define that the density dnorm is taken on (-Inf, Inf) Thanks a lot! Er, if you mean integral with respect to the x argument in dnorm, then the answer is -Inf because log(dnorm(x,...)) goes quadratically to -Inf in both directions. If you meant otherwise, please tell us what you meant... [[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] Shapiro-Wilk test problem
Hi everybody, Does anyone know what problem may be with this test. I am applying 5 different normality tests and use p-values for them, but for some reason S-W gives me NA, while sample size is 100. Any ideas? Thanks a lot! [[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] Fitting chi-squared distribution
Dear all, I have a question regarding performing test if the data fits chi-squared distribution. For example, using ks.test() I found in the examples how to fit it to gamma or weibull x-rnorm(100) ks.test(x, pweibull, shape=2,scale=1) for the gamma, pgamma can be used But I cannot find the value of this second parameter for the chi-squared distribution. Maybe someone experienced the similar problem? Thanks a lot! [[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] nls function
Hi everybody, would like to ask if there is any possibility to get SOME BEST result for nls fitting if the function itself doesn't produce it. I already increased number of iterations, reduced minfactor and still it doesn't give me the solution. Thanks a lot. [[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] Matrix elements are vectors
Dear all, My question how is it possible to define a matrix A with 10 rows 1 column, so that its elements are vectors of undefined length. I need to have a possibility later to add elements like A[1,1] - c(A[1,1],3,4,5) Thanks a lot for the help! [[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.