For those who (like me) swear by Lyx and its wonders, glad tidings are here (rather, they were already here, but I only noticed these tidings now!). It is possible to copy a figure (say, from a pdf document) and paste it directly into lyx as a figure! The process is as simple as "Paste Special-->png", but one is restricted to png and epg formats for the pasted objects (which most users ought to find adequate).
Friday, November 18, 2011
Saturday, September 11, 2010
Resources for texts in Statistics and Mathematics
For all those who (like me!) dream of building up a nice collection of statistics and mathematics texts, the Springer series texts are the stuff of which dreams,, and nightmares!, are at the same time made up of; the former due to the range of good books, the latter owing to the price-gouging indulged in by Springer.
Relief, however, is at hand to all such denizens, for Springer have now made available a (somewhat small) selection of their books available for download online, chapter-by-chapter. Find them at:
Wednesday, September 8, 2010
Reading in multiple files interactively with R
Many, like me, will have had occasion to do the following: read a few files (say .txt files, for instance) from a known folder, and then (possibly after some manipulations/computations) plot the contents in the files (or some transformations thereof). The important thing is that when these computations are repeated after a few days/weeks/months, we must again locate all the files (worry about whether those files are in the same folders etc) and since R is not really GUI, there is the pain of finding the snippets of code where the folder locations and file names are hard wired. A simpler means would allow the user to either specify a folder (but not the file names) or to even search for a folder in which to search for the required files, and that is what is described below.
Effectively, I will illustrate the R command "choose.files" (see here for more details) from "utils" package.
What I carry out below, using this command is to read in, from the folder specified below, many files (all of which have the same extension, .txt, in this example, although that is not necessary).
This snippet does the reading in the file paths part (the difficult part):
test=choose.files(default = "C:/MyFolder/*.*", caption = "Select files",
multi = TRUE, filters = Filters[c("txt", "All"),],
index = nrow(Filters))
where "default=C:\*.*" indicates the folder from which to read the files, and that all possible files in that folder are to be displayed. In this case, the option "filer" is actually redundant, and therefore, I could just as easily have written
test=choose.files(default = "C:/MyFolder/*.*", caption = "Select files",
multi = TRUE )
instead.
Of course, having read the full path of the files in, one can simply read the said files in as below:
NFiles=length(test)
Data=vector(mode="list",NFiles)
for (i in 1:NFiles) {
Data[[i]]=read.table(test[i],header=F)
}
As simple, and powerful, as that!
Wednesday, April 15, 2009
Reading multiple files in multiple folders
Here is another common problem faced by those reading in data generated by other packages, especially climate data, which arrive in multiple files, some times in multiple folders. To be more concrete, suppose there are "y" number of folders, each of which contain "x_y" number of files (allowing the number of files to vary by folder). Assuming that all files are of the same type (text, csv, matlab etc), the problem is to aggregate these files into one file (i.e. to generate one large file containing all the data).
Here is how one might go about it:
dirs=dir('*');
Dirnames={};
for i=1:numel(dirs)-2; % I do "-2" since the first 2 instances are always the hidden files
Dirnames{i}={dirs(i).name}; % creates as many directories as number of folders
end
for i=1:numel(dirs)-2
s = ['cd ' eval(['dirs(i+2).name']) ]; % the "+2" is to go the third folder, since first 2 are blank
eval(s) %changing directory each time to the required folder
filesStruct = dir('*.csv'); % collect list of all csv files in a folder
files = {filesStruct.name};
dataCell = cell(numel(files), 1); % create as many data cells as number of files in that folder
for j=1:numel(files)
dataCell{j} = load('-ascii',files{j}); %load data
end
Dirnames{i}=cat(1, dataCell{:}); % stack data one file below the other
cd ..
end
data=[];
data=cat(1,Dirnames{:}); % stack data one folder below the other
csvwrite('aggregate.csv',data);
The post above is really a modification of the one here
Here is how one might go about it:
dirs=dir('*');
Dirnames={};
for i=1:numel(dirs)-2; % I do "-2" since the first 2 instances are always the hidden files
Dirnames{i}={dirs(i).name}; % creates as many directories as number of folders
end
for i=1:numel(dirs)-2
s = ['cd ' eval(['dirs(i+2).name']) ]; % the "+2" is to go the third folder, since first 2 are blank
eval(s) %changing directory each time to the required folder
filesStruct = dir('*.csv'); % collect list of all csv files in a folder
files = {filesStruct.name};
dataCell = cell(numel(files), 1); % create as many data cells as number of files in that folder
for j=1:numel(files)
dataCell{j} = load('-ascii',files{j}); %load data
end
Dirnames{i}=cat(1, dataCell{:}); % stack data one file below the other
cd ..
end
data=[];
data=cat(1,Dirnames{:}); % stack data one folder below the other
csvwrite('aggregate.csv',data);
The post above is really a modification of the one here
Tuesday, October 28, 2008
Rotating tables
The issue of tables too broad to fit into one page led me to search for a solution to the issue, rotating the tables. Here is a simple recipe, and note that "pdflatex" appears to be compatible with rotating (at least, I was able to get it to work).
An example:
\documentclass[]{article}
\usepackage{rotating}
[some text...]
\begin{sidewaystable}
[body of table; can include \begin{tabular}]
\end{sidewaystable}
An example:
\documentclass[]{article}
\usepackage{rotating}
[some text...]
\begin{sidewaystable}
[body of table; can include \begin{tabular}]
\end{sidewaystable}
outreg2 and its usage with LaTeX
Here is a (necessarily personalised) view point on Stata's "outreg2" command, especially on its tex capabilities
- As its (rather copious) help indicates, when multiple regression outputs sought to be appended to ONE tex file, the tex command must be provided ONLY on the last instance of "outreg2".
Consider, for instance, 3 (trivial) regressions:
reg y x1 x2
outreg2 using example, replace
reg y x1 x2 x3
outreg2 using example, append
reg y x1 x2 x3 x4
outreg2 using example, append tex
This is the correct order of usage, if (as in many cases) one considers minor modifications to a single framework and desires the ouput in one tex file
- Variable labels: to preserve labels assigned to variables, note that appending "see labels" to the final instance of "outreg2" is mandatory, else the labels are not printed!
[This is independent of whether the output format is tex, word or excel]
To continue with the trivial example above:
I.
reg y x1 x2
outreg2 using example, replace see labels
reg y x1 x2 x3
outreg2 using example, append see labels
reg y x1 x2 x3 x4
outreg2 using example, append see labels
Note that ommitting the "see labels" in the first and second instance of "outreg2" will have no influence on labels being inserted in the file "example", while retaining "see labels" in the first two instances, dropping it on the third will ensure that labels are not printed!
- When using the "tex" option, it is probably better to use "tex(frag)" since if the desire is to finally collate output into one tex file, this option simply allows one to either "\include{}" or "\input" the relevant file into the Master tex file.
eg. [In the Master tex file]
\input table1
\input table2
and so on
[which will not work with the "tex" option unless one manually deletes the "\begin{document} and the \end{document}", along with any "\usepackages{}"]
- An issue with this approach is with tables too large (horizontally) to fit into one page (i.e. a few columns are truncated or cut-off), as is common with many economic regressions; an idea would be to rotate the tables to be oriented vertically; this maybe carried out in the Master tex file using "\usepackage{rotating}" and inserting a "\begin{sidewaystable}" before each of the tables manually--in the original files, NOT in the master file! [see the Latex-related "rotating tables" on this blog]
- directing the output to a file: especially tricky if the file is in a different folder; see under "Windows file names and Stata" on the blog
- As its (rather copious) help indicates, when multiple regression outputs sought to be appended to ONE tex file, the tex command must be provided ONLY on the last instance of "outreg2".
Consider, for instance, 3 (trivial) regressions:
reg y x1 x2
outreg2 using example, replace
reg y x1 x2 x3
outreg2 using example, append
reg y x1 x2 x3 x4
outreg2 using example, append tex
This is the correct order of usage, if (as in many cases) one considers minor modifications to a single framework and desires the ouput in one tex file
- Variable labels: to preserve labels assigned to variables, note that appending "see labels" to the final instance of "outreg2" is mandatory, else the labels are not printed!
[This is independent of whether the output format is tex, word or excel]
To continue with the trivial example above:
I.
reg y x1 x2
outreg2 using example, replace see labels
reg y x1 x2 x3
outreg2 using example, append see labels
reg y x1 x2 x3 x4
outreg2 using example, append see labels
Note that ommitting the "see labels" in the first and second instance of "outreg2" will have no influence on labels being inserted in the file "example", while retaining "see labels" in the first two instances, dropping it on the third will ensure that labels are not printed!
- When using the "tex" option, it is probably better to use "tex(frag)" since if the desire is to finally collate output into one tex file, this option simply allows one to either "\include{}" or "\input" the relevant file into the Master tex file.
eg. [In the Master tex file]
\input table1
\input table2
and so on
[which will not work with the "tex" option unless one manually deletes the "\begin{document} and the \end{document}", along with any "\usepackages{}"]
- An issue with this approach is with tables too large (horizontally) to fit into one page (i.e. a few columns are truncated or cut-off), as is common with many economic regressions; an idea would be to rotate the tables to be oriented vertically; this maybe carried out in the Master tex file using "\usepackage{rotating}" and inserting a "\begin{sidewaystable}" before each of the tables manually--in the original files, NOT in the master file! [see the Latex-related "rotating tables" on this blog]
- directing the output to a file: especially tricky if the file is in a different folder; see under "Windows file names and Stata" on the blog
Subscribe to:
Posts (Atom)