[R] Save data in txt
Hi everyone I have a little problem with R. I built a lineal regression equation using stepAIC function in both directions. Once I get this formula (lineal regression), I would like to save in a txt file data refered to p-value, r-squared, coefficients,...from the choosen model previously using stepAIC and p-value,r-squared, coefficients,...from each predictor. I try to use function "Write" to create a txt and save that data but I can't. So, Does anyone know a function to save specific data in a txt? I would like to save that specific data in separated columns, I mean, first column first predictor, second column p-value, third column r-squared, etc... Refered to data from each predictor (p-value and r-squared), is there an order in R to obtain this data or I have to calculte them by myself? I know I can use r.squared but it gives me a value from the choosen model not from an specific predictor. Cheers, Lucas _ [[elided Hotmail spam]] [[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] Variable as a filename
Hi R community, I have a question. I have 5 files in a directory. Each file has a year as a name (file 1 ->2004, file 2-> 2005, ...). I want to build a for loop where I call first file, do some calculations, go to second file, do some calculations, etc. Somethin like this: year<-2003 nfiles <- length(dir()) for( year in 2003:nfiles) {clima<-read.csv2([year]".csv", nrows=10) } As you see, file name has to change when I read a year, in other words, if a read 2004 in variable year, I need to select file 2004. Then I read 2005 in variable year, and then I R have to open file 2005,...That's my question, How can I write the order read.csv2 in a way to obtain the file correspondant to the year read in the for loop. Thanks in advance. Lucas _ [[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] P-value and R-squared variable selection criteria
Hi R community I have a question. I'll explain my situation. I have to build a climate model to obtain monthly and annual temperature from 2004 to 2008 from a specif area in Almeria (Spain). To build this climate model, I will use Multiple regression. My dependant variable will be monthly and annual temperature and independant variables will be Latitute, Longitude and Altitude and I will work with climate data from 10 climate stations distributed in my area of interest. I have to fit the climate model from the data to get temperature for each month. And I need to use p-value and r-squared adjusted from the model to obtain the best fit. I'll put an example. My climate data will be: V1 V2 V3 V4 V5 1 1 18 3 6 187 2 2 21 6 8 68 3 3 23 9 5 42 4 4 19 8 2 194 5 5 17 3 2 225 (V1 - climate station, V2 - temperature, V3 - Latitude, V4 - Longitude, V5 - Altitude) I fit the model to the data fit(V2~V3+V4+V5, data=clima) And I get Call: lm(formula = V2 ~ V3 + V4 + V5, data = clima) Residuals: 12345 0.24684 -0.25200 0.17487 -0.05865 -0.11107 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 22.103408 2.526638 8.748 0.0725 . V3 0.236477 0.152067 1.555 0.3638 V4 -0.073973 0.169716 -0.436 0.7383 V5 -0.024684 0.006951 -3.551 0.1748 --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1 Residual standard error: 0.4133 on 1 degrees of freedom Multiple R-squared: 0.9926, Adjusted R-squared: 0.9706 F-statistic: 44.95 on 3 and 1 DF, p-value: 0.1091 P- value for this model is 0.1091 However, I see that variable V4 has a really high p-value, so if I take it out, my model will have a better p-value. So: fit2<-lm(V2~V4+V5) Call: lm(formula = V2 ~ V4 + V5, data = clima) Residuals: 12345 0.28356 -0.21880 0.05952 0.40918 -0.53346 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 25.764478 1.199212 21.485 0.00216 ** V4 -0.278286 0.140452 -1.981 0.18606 V5 -0.034109 0.004451 -7.664 0.01660 * --- Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1 Residual standard error: 0.5403 on 2 degrees of freedom Multiple R-squared: 0.9748, Adjusted R-squared: 0.9497 F-statistic: 38.74 on 2 and 2 DF, p-value: 0.02516 My new p value for the model is lower, and better. So, this is what I have to do, I have to import climate data, and build the climate model using those independant variables that give me the best p-value for the model, and I have to do it automatic (since this example I did it manual). So, my question after all this long explanation. Is there a package u order I can download to apply selection of independent variables using as criteria p-value and adjusted R-squered, or on the contrary, I have to build what I need by myself. I guess I can build it by myself but it will take me a while but I would like to know if there is some package to help to do it faster. Well, thanks in advance. Lucas _ Nuevo Windows Live, un mundo lleno de posibilidades. Descúbrelo. http://www.microsoft.com/windows/windowslive/default.aspx [[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] Check value interval in a if loop
Hi R community I have a little problem, and I tried to solve it by myself but I couldn't. I building an if loop, and I want to check a value inside an interval. This would be the case: pvalue=0,2999 if(pvalue>0.05 or pvalue<0.1) as you can see I would like to check in that if loop if my pvalue is inside of that interval(from 0.05 to 0.1), and I tried these options: if(pvalue>0.05 or pvalue<0.1) (not possible because R don't recognise OR as operator) if(pvalue>0.05 || pvalue<0.1) (this one is not good enough, cause it fulfills one condition pvalue>0.05 but it doesn't get other contidion pvalue<0.1) does anyone know a way to stablish a inteval as an statement for my if loop. Thanks in advance Lucas _ [[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] Read header csv file
Hi R community, First of all, I want to thank everybody to share their time solving R questions, You are great. Ok, for my questions, I've been looking for a solutions by myself, in forums but I'm just a little bit desesperate so I hope somebody can help me. I have built a code to read files from a directory. These files are named by a year (2004.csv, 2005.csv,...). When the code reads first file (2004.csv), inside this file, there is information about precipitation of every months and I calculate different variables like R square adjusted, p value or formula fit to the data from linear regression. The code do more things but for my question, to explain what I need, that part of the code is enough. I want to export to the text file, year and month apart of some other variables, something like this: Year: 2004 Month: January R1: 0.98 Pvalue: 0.03 ... Year: 2004 Month: February R1:0.78 Pvalue:0.12 ... I've seen that I can use order sink() and cat(), so I would put those orders in my code, like this: nfiles<- length(dir("directory where my files are")) #Count file number for(year in 1:nfiles)#Read first file { filename<-dir()[[year]]#take first file and read filename, so if year is 1, then filename will be 2004, is year is 2, filename will be 2005,... clima<-read.csv2(filename, nrows=7) #open 2004.csv So, if want to export year to my text file I would do for(year in 1:nfiles)#Read first file { sink("directore where my text file is") filename<-dir()[[year]]#take first file and read filename, so if year is 1, then filename will be 2004, is year is 2, filename will be 2005,... cat(" Year: ",filename) sink() clima<-read.csv2(filename, nrows=7) #open 2004.csv And in my text file would read Year: 2004 Now, I want to the same to months. (I have built a for loop to read months inside for loop to read years). When I import a csv file I get something like this JanurayFebruary 13.0 4.1 21.4 3.7 3 0.2 1.5 4 6.7 4.1 . . . I can use commands like clima$Januray or clima[[1]] but I just get precipitation values. However, I am not able to get the header of the column. If I would able to do that I could do the same as for years and export those headers to my text file. Does anyone know how I could do that? or does anyone know another way to do what I need? Would anyone use sink() and cat() commands to create a summary text like the one I need to do?. Probably my for loop is not the best, I am still a beginner with R, and probably there are some better forms to express in R what I need but I am working alone so there is nobody in person to help me so I apologize for my simple questions. Thanks in advance. Lucas _ [[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] Interpolation
Hi R community I need to interpolate precipitation data for a natural park. I have precipitation data from some climate stationts. (I know the table is not complete but I only need to show you X,Y, Altitude and PrepJul) X Y Altitude PrepJan PrepFeb PrepMar PrepAp PrepMay PrepJun PrepJul 597706 4093438 41 0 9 77,8 63,1 17,5 0 2,6 597535 4088967 202 0 11,3 67,9 70,8 12,1 0 0,6 572307 4064892 41 1 9,5 22 40,5 2,5 0 0 571059 4074743 50 2,4 13,9 31,3 63,3 8,1 0 1,4 570795 4091537 356 0 15 79 86 19,5 1 0 554563 4077703 20 5,2 15,6 43,4 64,5 15 0,2 0 575091 4089921 183 1,2 13,2 64,6 70,6 16,2 0,2 3,4 The area where this data must be interpolated in the natural park with an irregular shape inside of a grid of 2190 lines and 2282 columns with utm coordinates. I have tried "Krig" from "fields" package. A created a matrix with independant data (a) and a vector with dependant data (b). a<-matrix(c(clima$X,clima$Y,clima$Altitud),,3) b<-clima$PrepJul Interpolation<-Krig(a,b) mapa<-predict.surface(interpolacion, nx=2190, ny=2282) str(mapa) num [1:2190] 554563 554583 554602 554622 554642 ... $ y: num [1:2282] 4064892 4064905 4064917 4064930 4064942 ... $ z: num [1:2190, 1:2282] NA NA NA NA NA NA NA NA NA NA ... NA values are due to the fact that outside of the natural park there isn't precipitation registred. NA values are not a problem. I write the result to ENVI with "write.ENVI" from "Catools" package. And the result is an image but data interpolated don't follow the natural park limits, the image of interpolated data don't macht the area of the natural park. And I don't know how to solve this. If anyone know any possible reason or any suggestion to do an interpolation, I would be really grateful. Lucas _ [[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] stepwise with F statistics
Hi Community R I need to make a stepwise using F statistics as a criteria to choose variables.I have 3 independant variables and one dependant variable, and I need to choose the best model fitting to my data using F statistics. The problem is I haven't found any package to do such operation. I have found packages to choose variables using significance level like "stepback" from "maSigPro" package. However, I haven't found something similar using with F-statistics. I tried grasp package as well but the documentation of this package is not clear. I read in several messages from the community that it's possible to do a stepwise using F statistics as criteria, but they don't specify how to do it, so, I would appreciate if anyone can help me with some indication about any package that could do what I need. Thanks in advance. Lucas _ us contactos? Revisa tu correo mientras conversas con tus amigos. [[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] Import/export ENVI files
Hi! I'm a beginner with this webpage so, I don't know if I'm sending my question to the correct site. Anyway, I'm working with R and I need to import and export ENVI files, (*.HDR files). A colleague told me that there is a package to import/export envi files but I haven't found that package, so does anyone know something about this? thank you so much :) . Ciaooo _ [[elided Hotmail spam]] [[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.