Re: [R] Help : glm p-values for a factor predictor
Thank you for your answer. The used code is the next one : champ_model<-c("y","categ_juridique","Indic_CTRLAUTRE_RPOS","Indic_CTRLAUTRE_RNEG","Indic_CTRLCCA_RPOS", "Indic_CTRLCCA_RNEG","Indic_CTRLCPAP_RPOS","Indic_CTRLCPAP_RNEG","Indic_CTRLLCTI_RPOS", "Indic_Changement_NomLogiciel","Indic_Changement_NomEditeur","Changt_NomEditeurPaie", "Changt_NomLogicielPaie","Infoabs_NomEditeurPaie","Infoabs_NomLogicielPaie", "Indic_Decla_comple","Indic_Decla_AnnuRempl","class_ape","class_Logiciel","class_Editeur", "moda_delai_soldeN_1","moda_delai_soldeN_2","moda_delai_soldeN_3","moda_delai_soldeN_4", "moda_delai_soldeN_5", "moda_anciennete_debitN_1","moda_anciennete_debitN_2","moda_anciennete_debitN_3", "moda_anciennete_debitN_4","moda_anciennete_debitN_5", "moda_moy_anciennete_debit","moda_std_anciennete_debit", "moda_moy_delai_solde","moda_std_delai_solde", var_cluster_Arome,var_cluster_BRC,var_cluster_Cedre,var_cluster_cntx2,var_cluster_ctrl, var_cluster_DADS_assiette2,var_cluster_DADS_avantage2,var_cluster_DADS_contrat2, var_cluster_DADS_salarie2,var_cluster_Sequoia) --> The predictors between quotes (excepted y) are qualitative ; others are groups of continuous predictors Var_model<-paste0("y ~ ", paste(champ_model_cont[-1],collapse=" + ")) Logit_appr<-glm(formula=Var_model,family=binomial(link="logit"),data=pop_ctrl_siren_cca2017_appr) --> The results of this glm do not provide overall pvalues for the qualitative predictors, only one pvalue by modality. And for selecting the qualitative predictors, i need that overall pvalue that SAS for example provides with PROC LOGISTIC. Benoit Pel�. De :"Bob O'Hara" <rni@gmail.com> A : Beno�t PELE <benoit.p...@acoss.fr>, Cc :r-help <r-help@r-project.org> Date : 29/06/2017 11:46 Objet : Re: [R] Help : glm p-values for a factor predictor It might help if you provided the code you used. It's possible that you didn't use direction="backward" in stepAIC(). Or if you did, it was still running, so whatever else you try will still be slow. The statement "R provides only the pvalues for each level" is wrong: look at the anova() function. Bob On 29 June 2017 at 11:13, Beno�t PELE <benoit.p...@acoss.fr> wrote: > Hello, > > i am a newby on R and i am trying to make a backward selection on a > binomial-logit glm on a large dataset (69000 lines for 145 predictors). > > After 3 days working, the stepAIC function did not terminate. I do not > know if that is normal but i would like to try computing a "homemade" > backward with a repeated glm ; at each step, the predictor with the max > pvalue would be excluded until reaching a set of 20 predictors for > example. > > My question is about the factor predictors with several levels. R provides > only the pvalues for each level whereas i need an overall pvalue for > testing the predictor. > > On internet, the only solution i found suggests to compute a Khi2 > log-likelihood test between the complete model and the model without the > factor predictor to emphasize its relevance. > > Do you know other ways? Another R package managing this kind of issue? > > Thank you and best regards, Benoit. > [[alternative HTML version deleted]] > > __ > R-help@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. -- Bob O'Hara NOTE NEW ADDRESS!!! Institutt for matematiske fag NTNU 7491 Trondheim Norway Mobile: +49 1515 888 5440 Journal of Negative Results - EEB: www.jnr-eeb.org [[alternative HTML version deleted]] __ R-help@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 : glm p-values for a factor predictor
Hello, i am a newby on R and i am trying to make a backward selection on a binomial-logit glm on a large dataset (69000 lines for 145 predictors). After 3 days working, the stepAIC function did not terminate. I do not know if that is normal but i would like to try computing a "homemade" backward with a repeated glm ; at each step, the predictor with the max pvalue would be excluded until reaching a set of 20 predictors for example. My question is about the factor predictors with several levels. R provides only the pvalues for each level whereas i need an overall pvalue for testing the predictor. On internet, the only solution i found suggests to compute a Khi2 log-likelihood test between the complete model and the model without the factor predictor to emphasize its relevance. Do you know other ways? Another R package managing this kind of issue? Thank you and best regards, Benoit. [[alternative HTML version deleted]] __ R-help@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.
Re: [R] For loop with ifelse help
Hi Sayan, This is exactly what I was looking for - it worked perfectly. Many thanks!! Also, thanks to everyone else for their suggestions. Pele -- View this message in context: http://r.789695.n4.nabble.com/For-loop-with-ifelse-help-tp2550547p2552388.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] For loop with ifelse help
Hello R users, I have 2 files (file1 and f2) and I am trying to sum columns 6:10 of a specific row in f2 and append it in file 1 if the state variable in file 1 equals the rowname in f2. Below is an example of the code I wrote using a for loop, but it not working (i.e it only works for the last number (10) in the loop). Can someone tell me how to fix? Many thanks ! file1 - data.frame(ID=seq(1:30), state=sample(1:10, 30, replace=TRUE)); file1 ID state 1 1 7 2 2 7 3 3 6 4 4 4 5 5 5 6 6 7 7 710 8 8 1 9 9 1 10 10 5 . file2 - matrix(seq(1:100),nrow=10) f2 - as.data.frame(file2); f2 V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 1 11 21 31 41 51 61 71 81 91 2 2 12 22 32 42 52 62 72 82 92 3 3 13 23 33 43 53 63 73 83 93 4 4 14 24 34 44 54 64 74 84 94 5 5 15 25 35 45 55 65 75 85 95 6 6 16 26 36 46 56 66 76 86 96 7 7 17 27 37 47 57 67 77 87 97 8 8 18 28 38 48 58 68 78 88 98 9 9 19 29 39 49 59 69 79 89 99 10 10 20 30 40 50 60 70 80 90 100 for (i in length(f2)) { file1$chksum - ifelse ((file1$state==rownames(f2)[i]), rowSums(f2[rownames(f2)[i], 6:10]), 0) } print(file1) ID state chksum 1 1 7 0 2 2 7 0 3 3 6 0 4 4 4 0 5 5 5 0 6 6 7 0 7 710400 8 8 1 0 9 9 1 0 10 10 5 0 11 1110400 12 12 9 0 13 1310400 14 14 9 0 15 15 5 0 16 16 3 0 17 17 1 0 18 18 7 0 19 19 7 0 20 20 2 0 21 21 3 0 22 22 8 0 23 23 8 0 24 24 4 0 25 25 6 0 26 26 6 0 27 27 3 0 28 28 3 0 29 29 5 0 30 30 5 0 -- View this message in context: http://r.789695.n4.nabble.com/For-loop-with-ifelse-help-tp2550547p2550547.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] For loop with ifelse help
Hi David - thanks for your suggestion, but I am trying to avoid doing any merging and sorting for this step because the real file I will be working with has about 20 million records. If I can get this loop or something similar to work will be good enough. thanks again.. -- View this message in context: http://r.789695.n4.nabble.com/For-loop-with-ifelse-help-tp2550547p2550656.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] Data decomposition
Hi R users, I have a time series variable that is only available at a monthly level for 1 years that I need to decompose to a weekly time series level - can anyone recommend a R function that I can use to decompose this series? eg. if month1 = 1200 I would to decompose so that the sum of the weeks for month1 equals 1200, etc.. Many thanks in advance for any help. -- View this message in context: http://www.nabble.com/Data-decomposition-tp22767614p22767614.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] Comparing data frames and keeping non-matches
Hi R users, I am trying to compare 2 data frames by subject and match and save the no matches to an object called nomatch, but I am getting unexpected results... Can anyone tell me how to correct the code to get the expected results shown in the last table? Many thanks in advance for your any help! library(reshape) year - c(2100:2110) x1 - c(F,T,T,F,F,F,T,F,T,T,F) df1 - data.frame(cbind(year, x1)) df1$subject - c(1,1,1,2,2,3,3,3,3,4,4) df1$match - 1; df1 df2 - data.frame(cbind(year, x1)) df2$subject - c(1:11) df2$match - 1; df2 key - c(subject, match) nomatch - subset(df2, is.element(df2[,key], df1[,key])==FALSE); nomatch rm(list=ls()) Unexpected Results year x1 subject match 1 2100 0 1 1 3 2102 1 3 1 5 2104 0 5 1 7 2106 1 7 1 9 2108 1 9 1 11 2110 0 11 1 Results I expected year x1 subject match 5 2104 0 5 1 6 2105 0 6 1 7 2106 1 7 1 8 2107 0 8 1 9 2108 1 9 1 10 2109 1 10 1 11 2110 0 11 1 -- View this message in context: http://www.nabble.com/Comparing-data-frames-and-keeping-non-matches-tp22460451p22460451.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] Comparing data frames and keeping non-matches
Hi All, I found a solution that give the correct answer.. year - c(2100:2110) x1 - c(F,T,T,F,F,F,T,F,T,T,F) df1 - data.frame(cbind(year, x1)) df1$subject - c(1,1,1,2,2,3,3,3,3,4,4) df1$match - 1 df1$key - paste(df1$subject, df1$match, sep=) ; df1 df2 - data.frame(cbind(year, x1)) df2$subject - c(1:11) df2$match - 1 df2$key - paste(df2$subject, df2$match, sep=) ; df2 nomatch - subset(df2, is.element(df2[,key], df1[,key])==FALSE); nomatch rm(list=ls()) Pele wrote: Hi R users, I am trying to compare 2 data frames by subject and match and save the no matches to an object called nomatch, but I am getting unexpected results... Can anyone tell me how to correct the code to get the expected results shown in the last table? Many thanks in advance for your any help! library(reshape) year - c(2100:2110) x1 - c(F,T,T,F,F,F,T,F,T,T,F) df1 - data.frame(cbind(year, x1)) df1$subject - c(1,1,1,2,2,3,3,3,3,4,4) df1$match - 1; df1 df2 - data.frame(cbind(year, x1)) df2$subject - c(1:11) df2$match - 1; df2 key - c(subject, match) nomatch - subset(df2, is.element(df2[,key], df1[,key])==FALSE); nomatch rm(list=ls()) Unexpected Results year x1 subject match 1 2100 0 1 1 3 2102 1 3 1 5 2104 0 5 1 7 2106 1 7 1 9 2108 1 9 1 11 2110 0 11 1 Results I expected year x1 subject match 5 2104 0 5 1 6 2105 0 6 1 7 2106 1 7 1 8 2107 0 8 1 9 2108 1 9 1 10 2109 1 10 1 11 2110 0 11 1 -- View this message in context: http://www.nabble.com/Comparing-data-frames-and-keeping-non-matches-tp22460451p22464615.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] Merge 10 data frames with 3 id columns that are common to all data frames
Hi R users, Can anyone share some example code using merge_all (from the reshape package) to merge 10 data frames into 1 file. Thanks in advance for any help! -- View this message in context: http://www.nabble.com/Merge-10-data-frames-with-3-id-columns-that-are-common-to-all-data-frames-tp22402493p22402493.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] Merge 10 data frames with 3 id columns that are common to all data frames
I tried using merge_all as shown below but I am getting an error ... can anyone tell me what I am doing wrong? The result table below is what I am looking for. DF1 - data.frame(var1 = letters[1:5], x = rnorm(5), y =2) DF2 - data.frame(var1 = letters[1:5], t = rnorm(5), u =2) DF3 - data.frame(var1 = letters[1:5], d = rnorm(5), e =31) DF4 - data.frame(var1 = letters[1:5], f = rnorm(5), o =11) DF_all - merge_all(DF1, DF2, DF3, DF4, by=var1 ) Error in fix.by(by.x, x) : 'by' must specify column(s) as numbers, names or logical Results I would like var1x y d e t u x a -1.725155 2 -0.4809731 0.0329682 -1.725155 b 0.7999832 2.32965 31 -0.385364 2 0.799983 c -1.387224 2 0.61761 31 0.9774042 -1.387224 d 0.6459462 0.46152 31 1.3345912 0.645946 e 0.0587832 -0.2531231 0.6316762 0.058783 baptiste auguie-2 wrote: Hi, Try this: DF1 - data.frame(var1 = letters[1:5], x = rnorm(5), y =2) DF2 - data.frame(var1 = letters[3:7], x = rnorm(5), y=3) DF3 - data.frame(var1 = letters[6:10], x = rnorm(5), y=0) # ... DF10 if you wish ( result - merge_all(list(DF1, DF2, DF3) )) save( result, file =merged.rda) I didn't know of this function, thanks. Similar solutions using base functions were proposed recently on http://wiki.r-project.org/rwiki/doku.php?id=tips:data-frames:merge and i've now added this for reference. baptiste On 8 Mar 2009, at 20:23, Pele wrote: Hi R users, Can anyone share some example code using merge_all (from the reshape package) to merge 10 data frames into 1 file. Thanks in advance for any help! -- View this message in context: http://www.nabble.com/Merge-10-data-frames-with-3-id-columns-that-are-common-to-all-data-frames-tp22402493p22402493.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. _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ 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. -- View this message in context: http://www.nabble.com/Merge-10-data-frames-with-3-id-columns-that-are-common-to-all-data-frames-tp22402493p22404595.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] Merge 10 data frames with 3 id columns that are common to all data frames
Perfect - many thanks!!! baptiste auguie-2 wrote: The function expects a list of data.frames as a first argument but you provided a data.frame instead, the others are interpreted as optional arguments to merge_recurse(). Try this instead, merge_recurse(list(DF1,DF2,DF3,DF4)) var1x yt u d ef o 1a 1.39679 2 -1.18377 2 -0.041194 31 -1.05526 11 2b -0.20453 2 0.22777 2 -0.543270 31 1.45777 11 3c -0.91446 2 -0.97843 2 -1.279132 31 1.88759 11 4d -1.18069 2 -0.23963 2 -0.064799 31 -0.85747 11 5e -1.24802 2 1.43072 2 0.183317 31 2.50352 11 HTH, baptiste On 8 Mar 2009, at 23:40, Pele wrote: I tried using merge_all as shown below but I am getting an error ... can anyone tell me what I am doing wrong? The result table below is what I am looking for. DF1 - data.frame(var1 = letters[1:5], x = rnorm(5), y =2) DF2 - data.frame(var1 = letters[1:5], t = rnorm(5), u =2) DF3 - data.frame(var1 = letters[1:5], d = rnorm(5), e =31) DF4 - data.frame(var1 = letters[1:5], f = rnorm(5), o =11) DF_all - merge_all(DF1, DF2, DF3, DF4, by=var1 ) Error in fix.by(by.x, x) : 'by' must specify column(s) as numbers, names or logical Results I would like var1x y d e t u x a -1.725155 2 -0.4809731 0.0329682 -1.725155 b 0.7999832 2.32965 31 -0.385364 2 0.799983 c -1.387224 2 0.61761 31 0.9774042 -1.387224 d 0.6459462 0.46152 31 1.3345912 0.645946 e 0.0587832 -0.2531231 0.6316762 0.058783 baptiste auguie-2 wrote: Hi, Try this: DF1 - data.frame(var1 = letters[1:5], x = rnorm(5), y =2) DF2 - data.frame(var1 = letters[3:7], x = rnorm(5), y=3) DF3 - data.frame(var1 = letters[6:10], x = rnorm(5), y=0) # ... DF10 if you wish ( result - merge_all(list(DF1, DF2, DF3) )) save( result, file =merged.rda) I didn't know of this function, thanks. Similar solutions using base functions were proposed recently on http://wiki.r-project.org/rwiki/doku.php?id=tips:data-frames:merge and i've now added this for reference. baptiste On 8 Mar 2009, at 20:23, Pele wrote: Hi R users, Can anyone share some example code using merge_all (from the reshape package) to merge 10 data frames into 1 file. Thanks in advance for any help! -- View this message in context: http://www.nabble.com/Merge-10-data-frames-with-3-id-columns-that-are-common-to-all-data-frames-tp22402493p22402493.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. _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ 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. -- View this message in context: http://www.nabble.com/Merge-10-data-frames-with-3-id-columns-that-are-common-to-all-data-frames-tp22402493p22404595.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. _ Baptiste Auguié School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag __ 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. -- View this message in context: http://www.nabble.com/Merge-10-data-frames-with-3-id-columns-that-are-common-to-all-data-frames-tp22402493p22404961.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
[R] Week value function
Hi R users, I am looking for a date function that will give the following: - The number-of-week value is in the range 01-53 - Weeks begin on a Monday and week 1 of the year is the week that includes both January 4th and the first Thursday of the year. If the first Monday of January is the 2nd, 3rd, or 4th, the preceding days are part of the last week of the preceding year. This is similar to the SAS's week function with option V. I am currently using : date - strptime(DATE, %d%B%Y) week - format(date, %W) but, I could not find an option for doing the above description automatically. Can anyone help? Thanks in advance for any help. -- View this message in context: http://www.nabble.com/Week-value-function-tp22389878p22389878.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] Week value function
Hi David - I will try that.. Thanks for your suggestion! David Winsemius wrote: I am not seeing anything but that proves nothing of course. You could write your own function and stick it in the .First of your .Rprofile files that get loads at startup. Details here: http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_first.html week.dBY - function(x) format(strptime(x, %d%B%Y) , %W) dt-07JAN2009 week.dBY(dt) [1] 01 # a character valued vector Gives 00 for 01JAN2009 but you can adjust that behavior to your specifications. You could also convert to numeric if desired: nweek.dBY - function(x) as.integer(format(strptime(x, %d%B%Y) , %W)) nweek.dBY(dt) [1] 1 -- David Winsemius On Mar 7, 2009, at 12:34 PM, Pele wrote: Hi R users, I am looking for a date function that will give the following: - The number-of-week value is in the range 01-53 - Weeks begin on a Monday and week 1 of the year is the week that includes both January 4th and the first Thursday of the year. If the first Monday of January is the 2nd, 3rd, or 4th, the preceding days are part of the last week of the preceding year. This is similar to the SAS's week function with option V. I am currently using : date - strptime(DATE, %d%B%Y) week - format(date, %W) but, I could not find an option for doing the above description automatically. Can anyone help? Thanks in advance for any help. -- View this message in context: http://www.nabble.com/Week-value-function-tp22389878p22389878.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. David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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. -- View this message in context: http://www.nabble.com/Week-value-function-tp22389878p22394294.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] Date conversion
Hi R users, I have a factor variable called date as shown below: Can anyone share the best / most efficient way to extract year and week (e.g. year = 2006, week = 52 for first record, etc..)? My data set has 1 million records. DATE 11DEC2006 11SEP2006 01APR2007 02DEC2007 Thanks in advance for any help! -- View this message in context: http://www.nabble.com/Date-conversion-tp22355788p22355788.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] Date conversion
Hi Uwe, You are correct - that was a type O (52) and thanks for you your suggestion that works.. Pele wrote: Hi R users, I have a factor variable called date as shown below: Can anyone share the best / most efficient way to extract year and week (e.g. year = 2006, week = 52 for first record, etc..)? My data set has 1 million records. DATE 11DEC2006 11SEP2006 01APR2007 02DEC2007 Thanks in advance for any help! -- View this message in context: http://www.nabble.com/Date-conversion-tp22355788p22356526.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] output formatting
Hi Kingsford - this is exactly what I am looking for... Many thanks!! Kingsford Jones wrote: I'm guessing you processed a data frame with the 'by' function. Rather than restructuring the by output, try using a different function on your data frame. For example #install.packages(doBy) summaryBy(breaks ~ tension + wool, data=warpbreaks, FUN=sum) tension wool breaks.sum 1 LA401 2 LB254 3 MA216 4 MB259 5 HA221 6 HB169 as opposed to with(warpbreaks, by(breaks, list(tension,wool), sum)) : L : A [1] 401 -- : M : A [1] 216 -- : H : A [1] 221 -- : L : B [1] 254 -- : M : B [1] 259 -- : H : B [1] 169 hth, Kingsford Jones On Wed, Mar 4, 2009 at 8:17 PM, Pele drdi...@yahoo.com wrote: Hi R users, I have an R object with the following attributes: str(sales.bykey1) 'by' int [1:3, 1:2, 1:52] 268 79 118 359 87 147 453 130 81 483 ... - attr(*, dimnames)=List of 3 ..$ GROUP: chr [1:3] III II I ..$ year : chr [1:2] 2006 2007 ..$ week : chr [1:52] 1 2 3 4 ... - attr(*, call)= language by.data.frame(data = vars, INDICES = bykey1, FUN = sum) sales.bykey1 --- GROUP: III year: 2007 week: 51 [1] 64 --- GROUP: II year: 2007 week: 51 [1] 17 --- GROUP: I year: 2007 week: 51 [1] 21 --- GROUP: III year: 2006 week: 52 [1] 14 --- GROUP: II year: 2006 week: 52 [1] 62 -- GROUP: I year: 2006 week: 52 [1] 10 Can anyone share the most efficient way to convert the output (sales.bykey1) above to look like this: GROUP Year week sales III 2007 51 64 II 2007 51 17 I 2007 51 21 III 2006 52 14 II 2006 52 62 I 2006 52 10 Many thanks in advance for any help! -- View this message in context: http://www.nabble.com/output-formatting-tp22344554p22344554.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-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. -- View this message in context: http://www.nabble.com/output-formatting-tp22344554p22345085.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] Formatting numeric values in a data frame
Hi R users, I have a data frame that contains 10K obs and 200 variables where I am trying to format the numeric columns to look like the output table below (format to 2 decimal places) but I am having no luck.. Can someone tell me the best way to accomplist this? Thanks in advance for any help! str(ad.test) 'data.frame': 10,000 obs. of 200 variables: $ ID : Factor w/ .. $ x1 : num 0.1123334 0.4778966.. $ x2 : num 0.2 0.2.. $ x3 : num 12.8 9.6. Data.frame ID x1 x2 x3 a1 0.0123334 0.2 12.8 b3 0.477896366 0.2 9.6 c1 0.477896366 0.2 9.6 d5 0.477896366 0.2 9.6 e1 0.477896366 0.2 9.6 f2 0.477896366 0.2 9.6 g6 0.3 0.2 9.6 h8 0.3 0.2 9.6 i2 0.3 0.2 9.6 k2 0.3 0.2 9.6 = Output: ID x1 x2 x3 a1 0.010.0012.33 b3 0.480.009.44 c1 0.480.009.44 d5 0.480.009.44 e1 0.480.009.44 f2 0.480.009.44 g6 0.330.009.44 h8 0.330.009.44 i2 0.330.009.44 k2 0.330.009.44 -- View this message in context: http://www.nabble.com/Formatting-numeric-values-in-a-data-frame-tp22212031p22212031.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] Formatting numeric values in a data frame
Jim - example is perfect - many thanks!!! Philipp - many thanks for your example also. jholtman wrote: This might do what you want: x - read.table(textConnection(ID x1 x2 x3 + a1 0.0123334 0.2 12.8 + b3 0.477896366 0.2 9.6 + c1 0.477896366 0.2 9.6 + d5 0.477896366 0.2 9.6 + e1 0.477896366 0.2 9.6 + f2 0.477896366 0.2 9.6 + g6 0.3 0.2 9.6 + h8 0.3 0.2 9.6 + i2 0.3 0.2 9.6 + k2 0.3 0.2 9.6), header=TRUE) closeAllConnections() x.c - x # make a copy x.c[] - lapply(x.c, function(.col){ + if (is.numeric(.col)) return(sprintf(%.2f, .col)) + else return(.col) + }) x.c ID x1 x2x3 1 a1 0.01 0.00 12.33 2 b3 0.48 0.00 9.44 3 c1 0.48 0.00 9.44 4 d5 0.48 0.00 9.44 5 e1 0.48 0.00 9.44 6 f2 0.48 0.00 9.44 7 g6 0.33 0.00 9.44 8 h8 0.33 0.00 9.44 9 i2 0.33 0.00 9.44 10 k2 0.33 0.00 9.44 On Wed, Feb 25, 2009 at 4:19 PM, Pele drdi...@yahoo.com wrote: Hi R users, I have a data frame that contains 10K obs and 200 variables where I am trying to format the numeric columns to look like the output table below (format to 2 decimal places) but I am having no luck.. Can someone tell me the best way to accomplist this? Thanks in advance for any help! str(ad.test) 'data.frame': 10,000 obs. of 200 variables: $ ID : Factor w/ .. $ x1 : num 0.1123334 0.4778966.. $ x2 : num 0.2 0.2.. $ x3 : num 12.8 9.6. Data.frame ID x1 x2 x3 a1 0.0123334 0.2 12.8 b3 0.477896366 0.2 9.6 c1 0.477896366 0.2 9.6 d5 0.477896366 0.2 9.6 e1 0.477896366 0.2 9.6 f2 0.477896366 0.2 9.6 g6 0.3 0.2 9.6 h8 0.3 0.2 9.6 i2 0.3 0.2 9.6 k2 0.3 0.2 9.6 = Output: ID x1 x2 x3 a1 0.01 0.00 12.33 b3 0.48 0.00 9.44 c1 0.48 0.00 9.44 d5 0.48 0.00 9.44 e1 0.48 0.00 9.44 f2 0.48 0.00 9.44 g6 0.33 0.00 9.44 h8 0.33 0.00 9.44 i2 0.33 0.00 9.44 k2 0.33 0.00 9.44 -- View this message in context: http://www.nabble.com/Formatting-numeric-values-in-a-data-frame-tp22212031p22212031.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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ 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. -- View this message in context: http://www.nabble.com/Formatting-numeric-values-in-a-data-frame-tp22212031p22213008.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
[R] Formula that includes previous row values
Hi R users, Is there an easy way in R to generate the results table below using table 1 and the formula (simplified version of the real problem)? It would be easy if I knew the R equivalent of SAS's retain function, but could not find one. Thanks in Advance for any help! table1: ID X2 X3 1.001.00 0 2.000.00 3.001.00 4.003058 5.000.00 6.006.00 Formula: X3 = x2 + (.24 * x3) where the values in the x3 column of the result table are retained from previous x3 rows.. Also the first x3 value is initialized to 0 to start e.g. for ID=1 we have 1 + .24(0)= 1.00 for ID=2 we have 0 + .24(1)= 0.24 for ID=3 we have 1 + .24(.24) = 1.06 for ID=4 we have 3058 + .24(1.06) = 3058.25 etc. Results: ID X2 x3 1.001.001.00 2.000.000.24 3.001.001.06 4.0030583058.25 5.000.00733.98 6.006.00182.16 -- View this message in context: http://www.nabble.com/Formula-that-includes-previous-row-values-tp22170010p22170010.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] Formula that includes previous row values
Hi Greg - this is perfect - Thank You! Also, thanks to everyone for the other suggestions Greg Snow-2 wrote: How about: x3 - cumsum( x2* 0.24^(5:0) ) / 0.24^(5:0) with the 5 replaced by the length -1 for the more general case. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Pele Sent: Monday, February 23, 2009 1:59 PM To: r-help@r-project.org Subject: [R] Formula that includes previous row values Hi R users, Is there an easy way in R to generate the results table below using table 1 and the formula (simplified version of the real problem)? It would be easy if I knew the R equivalent of SAS's retain function, but could not find one. Thanks in Advance for any help! table1: ID X2 X3 1.00 1.00 0 2.00 0.00 3.00 1.00 4.00 3058 5.00 0.00 6.00 6.00 Formula: X3 = x2 + (.24 * x3) where the values in the x3 column of the result table are retained from previous x3 rows.. Also the first x3 value is initialized to 0 to start e.g. for ID=1 we have 1 + .24(0)= 1.00 for ID=2 we have 0 + .24(1)= 0.24 for ID=3 we have 1 + .24(.24) = 1.06 for ID=4 we have 3058 + .24(1.06) = 3058.25 etc. Results: ID X2 x3 1.00 1.001.00 2.00 0.000.24 3.00 1.001.06 4.00 30583058.25 5.00 0.00733.98 6.00 6.00182.16 -- View this message in context: http://www.nabble.com/Formula-that- includes-previous-row-values-tp22170010p22170010.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-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. -- View this message in context: http://www.nabble.com/Formula-that-includes-previous-row-values-tp22170010p22173033.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] Whitening Time Series
Hi Bob - your suggesting worked out great... Many thanks! Also, thanks everyone for the other suggestions! Bob McCall wrote: Look in the package forecast for the function Arima. It will do what you want. It's different than arima function in the stats package. Bob Pele wrote: Hi R users, I am doing cross correlation analysis on 2 time series (call them y-series and x-series) where I need the use the model developed on the x-series to prewhiten the yseries.. Can someone point me to a function/filter in R that would allow me to do that? Thanks in advance for any help! -- View this message in context: http://www.nabble.com/Whitening-Time-Series-tp22041765p22066246.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] Whitening Time Series
Hi R users, I am doing cross correlation analysis on 2 time series (call them y-series and x-series) where I need the use the model developed on the x-series to prewhiten the yseries.. Can someone point me to a function/filter in R that would allow me to do that? Thanks in advance for any help! -- View this message in context: http://www.nabble.com/Whitening-Time-Series-tp22041765p22041765.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] Outlier Detection for timeseries
Hi Stephen, I am doing cross correlation analysis and I am trying to find a outlier detection function in R that can detect changes in the level of the response series that are not accounted for by the estimated model. Something that tells whether the changes are considered Additive Outliers, Level Shifts, or Temporary Changes... The output in the original not is what SAS produces and I was looking for something similar.. R is very new to me (4 weeks) hence still feeling my way around... Many thanks! Pele wrote: Hello R users, Can someone tell if there is a package in R that can do outlier detection that give outputs simiilar to what I got from SAS below. Many thanks in advance for any help! Outlier Details Approx Chi- Prob ObsTime ID Type Estimate Square ChiSq 12 12.00Additive 2792544.6 186.13.0001 13 13.00Additive 954302.1 21.23.0001 15 15.00Shift63539.3 9.060.0026 -- View this message in context: http://www.nabble.com/Outlier-Detection-for-timeseries-tp22008448p22012539.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] Outlier Detection for timeseries
Hi Hans - I tried your suggestion and it worked out well... Many thanks!! Also, thank to everyone else for their suggestions. Hans W. Borchers-4 wrote: Pele drdionc at yahoo.com writes: Hello R users, Can someone tell if there is a package in R that can do outlier detection that give outputs simiilar to what I got from SAS below. Many thanks in advance for any help! I guess you are talking about the OUTLIER procedure in SAS that attempts to detect 'additive outliers' and 'level shifts' in a 'response' series, the second following Jong Penzer's Diagnosing shocks in time series. I have not come across this method in R, but you might have a look into the 'robfilter' (Robust Time Series Filters) package with functions like 'dw.filter', 'adore.filter', or 'wrm.filter', see for instance dw.filter is suitable for extracting low frequency components (the signal) from a time series which may be contaminated with outliers and can contain level shifts. For this, moving window techniques are applied. If your time series is actually a response, you might prefer to look at the series of residuals instead. Outlier Details Approx Chi- Prob ObsTime ID Type Estimate Square ChiSq 12 12.00Additive 2792544.6 186.13 .0001 13 13.00Additive 954302.1 21.23 .0001 15 15.00Shift 63539.3 9.060.0026 __ 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. -- View this message in context: http://www.nabble.com/Outlier-Detection-for-timeseries-tp22008448p22017808.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] Outlier Detection for timeseries
Hello R users, Can someone tell if there is a package in R that can do outlier detection that give outputs simiilar to what I got from SAS below. Many thanks in advance for any help! Outlier Details Approx Chi- Prob ObsTime ID Type Estimate Square ChiSq 12 12.00Additive 2792544.6 186.13 .0001 13 13.00Additive 954302.1 21.23 .0001 15 15.00Shift63539.3 9.060.0026 -- View this message in context: http://www.nabble.com/Outlier-Detection-for-timeseries-tp22008448p22008448.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] Output results to a single postscript document
Hello R users, Below is the code and output of what I am trying to do. My goal is to insert/print all items in the chart function into a pdf document. Only the acf and pacf charts gets printed. Again, thanks in advance for any help I can get! options (scipen=999, digits=7) library(lmtest) library(dyn) tdata - ts(read.table(C:/test/data.csv ,sep = ,,header=TRUE)) print(tdata) res - function(dep, indep, fn, env = parent.frame()) { depnm- deparse(substitute(dep)) indepnm - deparse(substitute(indep)) fo - sprintf(%s ~ lag(%s, -1) + %s, depnm, depnm, indepnm) fo - as.formula(fo, env = env) mod - do.call(dyn$lm, list(fo)) chart - function(mod) { pdf(file=results.pdf) par(mfrow=c(2,1)) acf(residuals(mod)) pacf(residuals(mod)) summ- summary(mod) aic - AIC(mod) dev.off()} output - return(list(Charts=chart(mod))) } results - res(tdata[ ,Y], tdata[ ,X_VARY]) ; results output### options (scipen=999, digits=7) library(lmtest) library(dyn) tdata - ts(read.table(C:/test/data.csv ,sep = ,,header=TRUE)) print(tdata) Time Series: Start = 1 End = 20 Frequency = 1 Unit Y X_VARY K D_WAY 11 3 2 450 22 5 3 950 33 6 11 2250 44 8 4 7 2 55 9 11 11 2 66 12 13 13 2 77 23 25 12 2 88 22 30 31 3 99 23 3 3 3 10 10 19 21 2132 11 11 3 2 434 12 12 5 3 9 4 13 13 6 11 22 4 14 14 8 4 7 4 15 15 9 11 11 4 16 16 12 13 13 4 17 17 23 25 12 4 18 18 22 30 31 4 19 19 23 3 3 4 20 20 19 21 21 5 res - function(dep, indep, fn, env = parent.frame()) { +depnm - deparse(substitute(dep)) +indepnm - deparse(substitute(indep)) + fo - sprintf(%s ~ lag(%s, -1) + %s, depnm, depnm, indepnm) + fo - as.formula(fo, env = env) + mod - do.call(dyn$lm, list(fo)) + + chart - function(mod) { + pdf(file=results.pdf) + par(mfrow=c(2,1)) + acf(residuals(mod)) + pacf(residuals(mod)) + summ- summary(mod) + aic- AIC(mod) + dev.off()} + + output - return(list(Charts=chart(mod))) } results - res(tdata[ ,Y], tdata[ ,X_VARY]) ; results $Charts null device http://www.nabble.com/file/p21888211/results.pdf results.pdf Hello R users, I have been trying to output all my results (text, plots, etc) into the same postscript file as one document, but have been unable to...Can anyone help me improve my code below so that I can accomplish this? Currently I have to output them separately then piece them back together into one document.. [[elided Yahoo spam]] http://www.nabble.com/file/p21888211/data.csv data.csv -- View this message in context: http://www.nabble.com/Output-results-to-a-single-postscript-document-tp21884901p21888211.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] Printing all output (text ans plots) to the same postscript document
Hello R users, I have been trying to output all my results (text, plots, etc) into the same postscript file as one document, but have been unable to...Can anyone help me improve my simplified version of the code below so that I can accomplish this? Currently I have to output them separately then piece them back together into one document.. Thanks in Advance for any help! options (scipen=999, digits=7) library(foreign) library(Hmisc) library(prettyR) library(DAAG) library(lmtest) library(car) library(MASS) library(nlme) library(dyn) library(wle) library(lasso2) library(zoo) tdata - ts(read.table(C:/MI_COE/Marketing Mix/UK_Project/R_Code/testing/data.csv ,sep = ,,header=TRUE)) print(tdata) res - function(dep, indep, fn, env = parent.frame()) { depnm- deparse(substitute(dep)) indepnm - deparse(substitute(indep)) fo - sprintf(%s ~ lag(%s, -1) + %s, depnm, depnm, indepnm) fo - as.formula(fo, env = env) mod - do.call(dyn$lm, list(fo)) summ- summary(mod) chart - function(mod) { postscript(file=fig.ps) par(mfrow=c(2,1)) acf(residuals(mod)) pacf(residuals(mod)) dev.off()} output - return(list(Summary=summ, Chart=chart(mod))) } results - res(tdata[ ,Y], tdata[ ,X_VARY]) ; results -- View this message in context: http://www.nabble.com/Printing-all-output-%28text-ans-plots%29-to-the-same-postscript-document-tp21884853p21884853.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] Output results to a single postscript document
Hello R users, I have been trying to output all my results (text, plots, etc) into the same postscript file as one document, but have been unable to...Can anyone help me improve my code below so that I can accomplish this? Currently I have to output them separately then piece them back together into one document.. Thanks in Advance for any help! options (scipen=999, digits=7) library(foreign) library(Hmisc) library(prettyR) library(DAAG) library(lmtest) library(car) library(MASS) library(nlme) library(dyn) library(wle) library(lasso2) library(zoo) tdata - ts(read.table(C:/tmp/data.csv ,sep = ,,header=TRUE)) print(tdata) res - function(dep, indep, fn, env = parent.frame()) { depnm- deparse(substitute(dep)) indepnm - deparse(substitute(indep)) fo - sprintf(%s ~ lag(%s, -1) + %s, depnm, depnm, indepnm) fo - as.formula(fo, env = env) mod - do.call(dyn$lm, list(fo)) summ- summary(mod) chart - function(mod) { postscript(file=fig.ps) par(mfrow=c(2,1)) acf(residuals(mod)) pacf(residuals(mod)) dev.off()} output - return(list(Summary=summ, Chart=chart(mod))) } results - res(tdata[ ,Y], tdata[ ,X_VARY]) ; results -- View this message in context: http://www.nabble.com/Output-results-to-a-single-postscript-document-tp21884901p21884901.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] How to show variables used in lm function call?
Hello R users, I am new to R and am wondering if anyone can help me out with the following issue: I wrote a function to build ts models using different inputs, but when R displays the call for a model, I cannot tell which variables it is using because it shows the arguments instead of the real variables passed to the function. (e.g Call: lm(formula = dyn(dep ~ lag(dep, -1) + indep)) --- not what I want to see lm(formula = dyn(Y ~ lag(Y, -1) + XVARY)) - this is what I want to see (see simplified version of the code and output below) Thanks in advance for any help!! options (scipen=999, digits=7) library(Hmisc) library(DAAG) library(car) library(MASS) library(nlme) library(dyn) library(zoo) tdata - ts(read.table(C:/R/testing/data.csv ,sep = ,,header=TRUE)) print(tdata) coeff - function(dep, indep) { mod- dyn$lm(dep ~ lag(dep, -1)+ indep) summ - summary(mod) res- list(COEF=summ)} out- coeff(tdata[ ,Y], tdata[ ,XVARY]); out #output# print(tdata) Time Series: Start = 1 End = 20 Frequency = 1 Unit Y XVARY K DWAY 11 3 2 4 50 22 5 3 9 50 33 611 22 50 44 8 4 72 55 911 112 66 1213 132 77 2325 122 88 2230 313 99 23 3 33 10 10 1921 21 32 11 11 3 2 4 34 12 12 5 3 94 13 13 611 224 14 14 8 4 74 15 15 911 114 16 16 1213 134 17 17 2325 124 18 18 2230 314 19 19 23 3 34 20 20 1921 215 coeff - function(dep, indep) { + +mod- dyn$lm(dep ~ lag(dep, -1)+ indep) +summ - summary(mod) +res- list(COEF=summ) } out - coeff(tdata[ ,Y], tdata[ ,XVARY]); out $COEF Call: lm(formula = dyn(dep ~ lag(dep, -1) + indep)) Residuals: Min 1Q Median 3Q Max -10.7157 -2.5454 -0.2090 0.8359 7.3292 Coefficients: Estimate Std. Error t value Pr(|t|) (Intercept)2.6473 2.1952 1.206 0.24538 lag(dep, -1) 0.5506 0.1558 3.535 0.00275 ** indep 0.3033 0.1259 2.408 0.02845 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 4.643 on 16 degrees of freedom (2 observations deleted due to missingness) Multiple R-squared: 0.6679, Adjusted R-squared: 0.6264 F-statistic: 16.09 on 2 and 16 DF, p-value: 0.0001479 -- View this message in context: http://www.nabble.com/How-to-show-variables-used-in--lm-function-call--tp21814443p21814443.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.