[R] cum sums

2014-03-09 Thread Philip A. Viton


Suppose I have a dataframe beginning:

id  yr   val
a  1950   1
b  1950   10
a  1951   2

I'm trying to produce a table of cumulative sums
of val, disaggregated by id and then yr, so the result
should begin

id yr   cumval
a  1950  1
a  1951  3
b  1950  10

I've been trying to do this using aggregate
and passing the function cumsum, but I can't get
it to work. Can someone tell me how to do this?
Thanks!



Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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] subset of factors in a regression

2013-07-01 Thread Philip A. Viton


suppose state is a variable in a dataframe containing abbreviations 
of the US states, as a factor. What I'd like to do is to include 
dummy variables for a few of the states, (say, CA and MA) among the 
independent variables in my regression formula. (This would be the 
equivalent of, creating, eg, ca-state==CA) and then including 
that). I know I can create all the necessary dummy variables by using 
the outer function on the factor and then renaming them 
appropriately; but is there a solution that's more direct, ie that 
doesn't involve a lot of new variables?


Thanks!



Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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] spdep::poly2nb problem

2011-05-07 Thread Philip A. Viton



I downloaded a US states+territories shapefile from
http://www.weather.gov/geodata/catalog/national/html/us-state.htm
and unzipped it into my working directory.
Then in R (version 12.2.0 for ms-win32):

library(rgdal) # Version: 0.6-28
library(spdep) # Version: 0.5-24 (loads sp: Version: 0.9-72)

# read in the data
states-readOGR(.,s_01de10)
summary(states)

# now create a neighbors list
states_nb-poly2nb(states)
# create a 0/1 matrix of the neighbors
nb_mat-nb2mat(states_nb,style=B,zero.policy=TRUE)
# add names
nms-as.character(states$NAMES)
# set the matrix names
dimnames(nb_mat)-list(nms,nms)

# now see which states have no neighbors
rowSums(nb_mat) 
# it turns out that Florida seems to have zero neighbors.

I then repeated it with
states_nb-poly2nb(states,useC=FALSE)

and this time -- the computation takes a lot longer, of course
--- Florida has 2 neighbors (correctly: AL and GA).


Can anyone tell me what's going wrong here?


Thanks!



Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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] makign help files by hand

2010-06-09 Thread Philip A. Viton


Can someone tell me how to make up (eg) a library's html help files 
by hand? I think I ought to be able to use RCMD Rdconv for this but 
(R-2.10.0, MS-win) when I type (in a dos session) rdcmd rdconv 
--help I get a message to the effect that a perl script rdconv can't 
be opened.


Can I do this from within R itself? And if so, how (in particular, 
what is the target file to be converted)?


Thanks!


Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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] convenience question

2009-10-04 Thread Philip A. Viton


On: R 2.8.1 / Ms Windows / R-Gui-R-console

I have a long-ish function, and to re-assure myself that it's 
actually making progress I've arranged a set of messages to the 
console (using cat). But what happens is that the messages don't 
actually display until the function  has finished, which sort of 
negates the purpose. However, if I hit Return after starting the 
function, then all is well. I'm pretty sure that what's happening is 
that the function isn't yielding time to the operating system in 
order that messages be processed: so my question is, is there any way 
to do that within the function? Or maybe, to simulate the Return keystroke?


Thanks!


Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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] customizing RGui/Rconsole

2009-08-23 Thread Philip A. Viton


Is it possible to change the behavior of RGui/RConsole under MS 
Windows so that, if you submit a bunch of commands from a script 
window, when they've finished, focus is given to the Console, and not 
(as now) to the script?



Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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 a dataframe summary to disk

2009-08-18 Thread Philip A. Viton


I'd like to write the summary of a dataframe to disk, so that it looks
essentially the same as what you'd see on screen; but I can't seem to 
do it. Can someone tell me how? Thanks!




Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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] R2winbugs : vectorization

2008-12-17 Thread Philip A. Viton


Many thanks to all responders. It turns out that there is a winbugs 
update available, which defines a new version of inprod, inprod2. 
Using inprod2 the vectorized code runs in about the same time as the 
scalar version.


On the print problem: the issue here turns out to be that the arm 
package (or one of the packages it loads) resets digits to 2. So the 
solution is to reset digits ( options(digits=5) or whatever) after 
loading your packages.


Thanks again!



Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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] R2winbugs : vectorization

2008-12-15 Thread Philip A. Viton


I'm new to bugs, so please bear with me. Can someone tell me if the 
following two models are doing the same thing? The reason I ask is 
that with the same data, the first (based on 4 separate coeffs 
a1--a4) takes about 50 secs, while the second (based on a vectorized 
form, a[]) takes about 300. The means are about the same, though 
R-hat's in the second version are quite a bit better.



(Also, and completely unrelated: is there any way to get more than 2 
decimal places in the display of the means?)



Thanks!!



Here are the two models: (these are censored regressions, the first 
is essentially a copy of code in Gelman+Hill):


= model 1 : 4 separate a's
model{
 for (i in 1:n){
   z.lo[i]- C * equals(y[i],C)
   z[i]~dnorm(z.hat[i],tau.y)I(z.lo[i],)
   z.hat[i]-a1*x[i,1]+a2*x[i,2]+a3*x[i,3]+a4*x[i,4]
 }
a1~dunif(0,100)
a2~dunif(0,100)
a3~dunif(0,100)
a4~dunif(0,100)
tau.y-pow(sigma.y,-2)
sigma.y~dunif(0,100)
}


== model 2 : vector of a's
model{
 for (i in 1:n){
   z.lo[i]- C * equals(y[i],C)
   z[i]~dnorm(z.hat[i],tau.y)I(z.lo[i],)
   z.hat[i]-inprod(a[],x[i,])
 }
 for (j in 1:k){
   a[j]~dunif(0,100)
 }
tau.y-pow(sigma.y,-2)
sigma.y~dunif(0,100)
}


and here, for reference, is the R calling code:

x-as.matrix(iv)
y-dv
C-cens
z-ifelse(y==C,NA,y)
n-length(z)
data1-list(x=x,y=y,z=z,n=n,C=C)
inits1-function(){
  list(a1=runif(1),a2=runif(1),a3=runif(1),a4=runif(1),sigma.y=runif(1))}
params1-c(a1,a2,a3,a4,sigma.y)

## now the bugs call for model 1
proc.time()
aasho.1-bugs(data1,inits1,params1,aasho1.bug,n.iter=1,debug=FALSE)
proc.time()
print(aasho.1,digits=4)

now we try a vector approach
k-4 # niv
data2-list(x=x,y=y,z=z,n=n,C=C,k=k)
inits2-function(){
  list(a=runif(k),sigma.y=runif(1))}
params2-c(a,sigma.y)

## now the bugs call for model 2
proc.time()
aasho.2-bugs(data2,inits2,params2,aasho2.bug,n.iter=1,debug=FALSE)
proc.time()
print(aasho.2,digits=6)


Philip A. Viton
City Planning, Ohio State University
275 West Woodruff Avenue, Columbus OH 43210
vito...@osu.edu

__
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.