[R] A couple of little R things I can't figure out (column percents, regression with lagged variables)

Paul Johnson pauljohn at ku.edu
Tue Feb 12 07:37:55 CET 2002


Simple usage questions that I ought to be able to figure on my own, but 
can't.

1.I'm able to produce a cross tabulation table showing counts with 
either table or xtabs.  But I want column percentages for 
interpretation, and it seems stupid to sit there with a calculator 
figuring marginals and column percentages.  How to make R do it after this:

 > x <- c(1,3,1,3,1,3,1,3,4,4)
 > y <- c(2,4,1,4,2,4,1,4,2,4)
 > hmm <- table(x,y)
 > hmm
    y
x   1 2 4
   1 2 2 0
   3 0 0 4
   4 0 1 1

#I can get the column sums:
 > tots <- apply(hmm,2,sum)

and I can get the total N of counts, but don't understand how to make it 
calculate column percents, as in

     y

x   1    2     4
   1 100 66.67  0
   3  0   0     80
   4  0   33.33 20

Pointers appreciated.


2. A student said here's  y, a vector representing a time series, and 
here's x, a vector representing a time series. I want to do a 
conventional regression of y on the lag of x. In sas you do xlag=lag(x) 
and then use xlag in a regresson.  I just want something simple like 
lm(y~lag(x)). But in R base there's no lag.

So I can get it the old fashioned way:
 > xx <- c(NA,x)
 > modl <- lm(y~xx[1:length(y)])
 > summary(modl)

One sidenote is that summary does not include any mention of the fact 
that 1 observation was lost due to missing value. That seems bad to me.

I see the lag function in ts, but when I use it, it doesn't change x, so 
obviously I don't see the point of that.

 > z <- lag (x)
 > z
  [1] 1 3 1 3 1 3 1 3 4 4


So long, thanks in advance, greetings, etc...

-- 
Paul E. Johnson                       email: pauljohn at ukans.edu
Dept. of Political Science            http://lark.cc.ku.edu/~pauljohn
University of Kansas                  Office: (785) 864-9086
Lawrence, Kansas 66045                FAX: (785) 864-5700

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list