> MLest<- arima(X, order = c(1,0,0), method = c("ML"))
> MLest
Call:
arima(x = X, order = c(1, 0, 0), method = c("ML"))
>Coefficients:
ar1 intercept
0.2657 -0.0824
0.0680 0.1018
sigma^2 estimated as 1.121: log likelihood = -295.23, aic = 596.47
I would like to write the 0.2657 and 1.121 results to an output file. I have defined a path and file name and here is my codes.
When I use, write(MLest, file=filename, append=TRUE, sep="\t") I got the following error:
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'
When I use, write.table(MLest[1:2], file=filename,sep=" ", col.names = F, row.names = F)
It works but I have:
0.265705946688229 1.12087092992291
-0.0823543583874666 1.12087092992291
I would like to have a result as:
0.265705946688229 -0.0823543583874666 1.12087092992291 (each value to different columns)
What should I use?
write.table is a bit of an overkill for writing a single line in a file. I'd recommend you use cat directly on a vector. As you can see from the error message, that's what write.table uses under the hood. This works:
cat(with(MLest, c(coef, sigma2)), "\n", sep = "\t",
file = filename, append = TRUE)
But I will point out: every time you run this command, a file handle is created, moved to the end of the file, a new line is written, then the filehandle is closed. It is quite inefficient and you'd better open a file connection instead:
fh <- open(filename)
for (...) {
MLest <- arima(...)
cat(with(MLest, c(coef, sigma2)), "\n", sep = "\t", file = fh)
}
close(fh)
This way, only one filehandle is created and it always points to the end of your file.
Alternatively, you could wait to have all your arima outputs to create a whole data.frame or matrix of coefficients and only then print it with a single call to write.table.
Assuming you have built a list ll of arima outputs, you can create and write that matrix of coefficients by doing:
output.mat <- t(sapply(ll, with, c(coef, sigma2 = sigma2)))
write.table(output.mat, file = "test.csv", row.names = FALSE)
Try
write.table(unique(as.vector(MLest[1:2])), file=filename,sep=" ", col.names = F, row.names = F)
Related
This is the code I am using, this creates a summary of stastistical tests I am interested in
library(ape)
library(geiger)
library(caper)
taxatree <- read.tree("newicktest.tre")
LWEVIYRcombodata <- read.csv("LWEVIYR.csv")
LWEVIYRcombodataPGLS <-data.frame(Sum.of.percentage=LWEVIYRcombodata$Sum.of.percentage,OGT=LWEVIYRcombodata$OGT, Species=LWEVIYRcombodata$Species)
LWEVIYRcombodataPGLS$Species<-gsub(" ", "", LWEVIYRcombodataPGLS$Species)
comp.dat <- comparative.data(taxatree, LWEVIYRcombodataPGLS, "Species")
phylo.signal <- pgls((Sum.of.percentage) ~1, data=comp.dat, lambda="ML")
I call this up with the line:
summary(phylo.signal)
And then save it as a variable:
dataforexport <- summary(phylo.signal)
However, this won't let me print it to a regular text file, or even a csv.
When trying something along the lines of this:
write.table(dataforexport, file = "test1.txt", sep = "\t", row.names = FALSE)
I get an error message:
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class ""summary.pgls"" to a data.frame
I want to be able to save the results of my analysis to a text file, or even a csv (ideally appending the file, but that's another question that's probably been answered somewhere else)
Give this a try -
capture.output(summary(phylo.signal), file = "test1.txt")
I need to add 2 columns to a list of csv files and then write the csv's again into a folder. So, what I did is I used llply.
data_files <- list.files(pattern= ".csv$", recursive = T, full.names = F)
x <- llply(data_files, read.csv, header = T)
y <- llply(x, within, Cf <- var1 * 8)
z <- llply(y, within, Pc <- Cf + 1)
When I tried to write the files again using write.table in a loop:
lapply(z, FUN = function(eachPath) {
b <- read.csv(eachPath, header = F)
write.table(b, file = eachPath, row.names = F, col.names = T, quote = F)
})
I get this error and I think it is because z is a list of lists.
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
'file' must be a character string or connection
What I think it needs to be done is to convert z in a list of dataframes. I would like and advise of how to do that, plus adding a command to extract the name of each file from a column containing the sample ID.
Thanks
I've identified key variables, I've trained a multiple regression model based on the variables, I've brought in a file that holds all the necessary variables (it's the full universe though (~290k vs 2.2k that the model was built on)), I've successfully generated a prediction based on the model. Now I just want to write the predictions to either the big file or to a separate file that I can then merge with the big file.
Here's the script I used to generate the predictions:
predict(fit1, Modeled, se.fit = FALSE, scale = NULL, df = Inf,
interval = c("none", "confidence", "prediction"),
level = 0.95, type = c("response", "terms"),
terms = NULL, na.action = na.pass,
pred.var = res.var/weights, weights = 1)
I've tried cbind and a variety of other things I found while Googling around but can't get any of them to work. Would someone mind helping out?
Update:
Here's the save command that I last tried to use.
save(fit1, list = character(),
file = stop(fit1),
ascii = FALSE, version = NULL, envir = parent.frame(),
eval.promises = TRUE, precheck = TRUE)
It generated the following error.
Error in save(fit1, list = character(), file = stop(fit1), ascii = FALSE, :
Which was followed by a short list of some of the predictions.
Update:
Ran this script:
save(fit1, list = character(),
file = "fit1",
ascii = FALSE, version = NULL, envir = parent.frame(),
eval.promises = TRUE, precheck = TRUE)
save.image(file = ".RData", version = NULL, ascii = FALSE,
compress = !ascii, safe = TRUE)
Which returned this error:
Error in save(list = names(.GlobalEnv), file = outfile, version = version, :
object 'ascii' not found
Warning in file.remove(outfile) :
cannot remove file '.RDataTmp', reason 'No such file or directory'
I'm pretty sure that the first error is the real issue here because the second command won't run successfully without the first command having worked.
A minimal reproducible example would help in answering the question. Also, you said you wanted to save the predictions, however your save command is being passed the fit1 lm object, and not the output of predict. I assume fit1 is an lm object because it is the first argument you are passing to predict.
However assuming you want to save the vector output of predict you can simply write it to a csv file.
x <- rnorm(100)
y <- rnorm(100)
fit1 <- lm(x ~ y)
write.csv(predict(fit1), file = "predictions.csv")
EDIT: Assuming from your comment you want the predictions combined with the "big file".
x <- rnorm(100)
y <- rnorm(100)
fit1 <- lm(x ~ y)
Modeled <- matrix(rnorm(1000), nrow = 100, ncol = 10)
output <- cbind(predict(fit1), Modeled)
write.csv(output, file = "predictions.csv", row.names = FALSE)
I would be delighted and most grateful if anyone can explain to me why I am having a problem exporting some data from a function which extracts coefficients from a linear model. I have hundreds to do so I’m hoping to build a loop to handle it but have fallen at an earlier hurdle.
I am using methods borrowed from someone much smarter at this stuff:
https://stat.ethz.ch/pipermail/r-sig-ecology/2008-May/000062.html
The relevant bits (data creation, the function and finally my attempt to export my data are below) but firstly I will mention that the data, “export”, is exported to the experiment.csv file as a single COLUMN. I am told that the Append property of the write.table function only works with rows. Consequently it overwrites previous runs of the same sets of commands rather than successfully appending it.
The error messages are of the form below: (they are all the same, one for each piece of information).
Warning messages:
1: In write.csv(export, file = "experiment.csv", append = TRUE, quote = TRUE, :
attempt to set 'append' ignored
#DATA CREATION
# create an empty list
mod <- list()
# start a loop for create 5 objects of class 'lm'
for (i in 1:5) {
x <- rnorm(i*10)
y <- rnorm(i*10)
mod[[paste("run",i,sep="")]] <- lm(y ~ x)
}
# FUNCTION TO EXTRACT DATA
myFun <-
function(lm)
{
out <- c(lm$coefficients[1],
lm$coefficients[2],
length(lm$model$y),
summary(lm)$coefficients[2,2],
pf(summary(lm)$fstatistic[1], summary(lm)$fstatistic[2],
summary(lm)$fstatistic[3], lower.tail = FALSE),
summary(lm)$r.squared)
names(out) <- c("intercept","slope","n","slope.SE","p.value","r.squared")
return(out)}
# FAILED ATTEMPT TO EXPORT
export <-myFun(mod$run1)
write.csv(export, file = "experiment.csv", append = TRUE, quote = TRUE, sep = " ",
eol = "\n", na = "NA", dec = ".", row.names = FALSE,
col.names = FALSE, qmethod = c("escape", "double"),
fileEncoding = "")
How to write to a csv file in R, in a similar way to what I did in Python?
In Python, I did this:
import os
import csv
import numpy as np
#sample data
x = np.asarray((1,2,3))
y = np.asarray((4,5,6))
filename = os.path.normpath("output_py.csv")
f = open(filename, "wb")
writer = csv.writer(f,delimiter=',')
writer.writerow(x)
writer.writerow(y)
f.close()
writer.writerow(array) works well because I don't need to replicate parameters when writing. All I need to do is specify the array to write to.
I would like to do the same in R. Here is what I have done thus far:
#sample data
x <- as.matrix(c(1,2,3))
y <- as.matrix(c(4,5,6))
filepath <- file.path("output_R.csv")
fileConn <- file(filepath)
#To write over previous files - only for first line
write.table(as.matrix(t(x)), sep=",", filepath, col.names=FALSE, row.names=FALSE)
#Thereafter append
write.table(as.matrix(t(y)), sep=",", filepath, col.names=FALSE, row.names=FALSE, append=TRUE)
close(fileConn)
Both methods produce the correct output,
1,2,3
4,5,6
but the problem is that each time I use write.table I need to replicate the parameters.
How to write rows without having to replicate parameters each time?
p.s. Any other suggestions for code optimisation will be appreciated.
Just write your own function write.row, that uses the parameters that you would like as standards, e.g.
write.row <- function(x, file = filepath, append = TRUE, quote = TRUE, sep=",",
eol = "\n", na = "NA", dec = ".", row.names = FALSE, col.names = FALSE,
qmethod = c("escape", "double"), fileEncoding = ""){
write.table(as.matrix(t(x)), file, append , quote, sep,
eol, na, dec, row.names, col.names,
qmethod, fileEncoding)
}
write.row(x, append=FALSE)
write.row(y)
In this case, I assumed that you want to call this function many times and would like to use append=TRUE as the default and change it for the first row. Otherwise just set the default to FALSE.