Folks, I have a strange situation, which I may have isolated as a bug report. Or, it could just be that there's something about R that I don't know. :-) I have attached the data file and the program file but don't know whether these attachments will make it into the list. Here is my bugreport.R program -- --------------------------------------------------------------------------- buoyancy <- function(year, taxbase, tax, description, plotname) { cat("Simple full OLS regression with all data:\n") logtax = log(tax) logtaxbase = log(taxbase) m = lm(logtax ~ logtaxbase) summary.lm(m) details = summary.lm(m) } A <- read.table(file="amodi-data.csv", sep=",", col.names=c("year", "gdp.ag", "gdp.mining", "gdp.manuf", "gdp.elecgas", "gdp.construction", "gdp.industry", "gdp.services", "gdp.fc", "indirect.taxes", "subsidies", "j1", "gdp.mp", "gdp.mp.93", "gdp.deflator", "gdp.fc.93", "gdp.ag.93", "gdp.industry.93", "gdp.services.93", "tax.income", "tax.corporation", "tax.direct.others", "tax.direct", "tax.customs", "tax.excise", "tax.indirect.others", "tax.indirect", "tax.total")) A = subset(A, !is.na(A$tax.total)) buoyancy(A$year, A$gdp.mp, A$tax.income, "Personal income tax and GDPmp", "p1") --------------------------------------------------------------------------- This program does not work. The summary.lm(m) statement seems to have no effect. When I run it, I get: $ R --slave < bugreport.R Simple full OLS regression with all data: where it is asif the summary.lm(m) statement never occurred. If I put in a statement print(m) it works, but the summary.lm(m) does not work. Now here's what's weird: Suppose I remove the statement that comes AFTER this summary.lm(m) statement. That is, I don't say details = summary.lm(m) as the last line of the function. In this case, the program works fine! I'm most confused. I can't see how putting in an assignment statement AFTER a function call can contaminate a PREVIOUS statement. I would be most happy if you could guide me... I am running on a nicely-working notebook which runs Debian linux kernel 2.4.17, and have R 1.8.1 (2003-11-21). I use the `testing' branch of Debian. -ans. -- Ajay Shah Consultant ajayshah at mayin.org Department of Economic Affairs http://www.mayin.org/ajayshah Ministry of Finance, New Delhi -------------- next part -------------- buoyancy <- function(year, taxbase, tax, description, plotname) { cat("Simple full OLS regression with all data:\n") logtax = log(tax) logtaxbase = log(taxbase) m = lm(logtax ~ logtaxbase) summary.lm(m) details = summary.lm(m) } A <- read.table(file="amodi-data.csv", sep=",", col.names=c("year", "gdp.ag", "gdp.mining", "gdp.manuf", "gdp.elecgas", "gdp.construction", "gdp.industry", "gdp.services", "gdp.fc", "indirect.taxes", "subsidies", "j1", "gdp.mp", "gdp.mp.93", "gdp.deflator", "gdp.fc.93", "gdp.ag.93", "gdp.industry.93", "gdp.services.93", "tax.income", "tax.corporation", "tax.direct.others", "tax.direct", "tax.customs", "tax.excise", "tax.indirect.others", "tax.indirect", "tax.total")) A = subset(A, !is.na(A$tax.total)) buoyancy(A$year, A$gdp.mp, A$tax.income, "Personal income tax and GDPmp", "p1")
Ajay Shah wrote:> where it is asif the summary.lm(m) statement never occurred. If I put > in a statement print(m) it works, but the summary.lm(m) does not work. > > Now here's what's weird: Suppose I remove the statement that comes > AFTER this summary.lm(m) statement. That is, I don't say > details = summary.lm(m) > as the last line of the function. In this case, the program works fine! > > I'm most confused. I can't see how putting in an assignment statement > AFTER a function call can contaminate a PREVIOUS statement. I would be > most happy if you could guide me...See the FAQ about summary() methods, and what they do. http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why%20is%20the%20output%20not%20printed%20when%20I%20source()%20a%20file%3f Cheers Jason
Ajay Shah writes: > buoyancy <- function(year, taxbase, tax, description, plotname) { > cat("Simple full OLS regression with all data:\n") > logtax = log(tax) > logtaxbase = log(taxbase) > m = lm(logtax ~ logtaxbase) > summary.lm(m) > details = summary.lm(m) > } > This program does not work. The summary.lm(m) statement seems to have > no effect. When I run it, I get: > > $ R --slave < bugreport.R > Simple full OLS regression with all data: > > where it is asif the summary.lm(m) statement never occurred. If I put > in a statement print(m) it works, but the summary.lm(m) does not work. > Ajay, the short answer is: the summary.lm() step is carried out all right, you just never get to see the result, because it is not written to the console. If you want it, just wrap it in an explicit print() call. This is completely standard behaviour. If you have myf <- function() {2; 10} the call myf() will return the value 10, but the value 2 will not be seen anywhere. While myg <- function() {print(2); 10} will write both the value 2 and the value 10 to the console. The reason you get a useful response from your function if you delete the 'detail =...' line, is because it makes the call to summary.lm() the last line of the function and hence returns it. Effectively making summary.lm() correspond to 10 in the above toy functions, instead of corresponding to 2. There is a more tricky answer to your questions: As you have written the function, it actually returns the results from the summary.lm() call - but it is deliberatly not printed! What the function returns, is the value of the last statement, which happens to be an assignment. The value of an assignement is the value that is being assigned. But the default printing behaviour of an assignement is to be silent. If you write x <- 1 at the prompt, seemingly nothing is returned... But actually the expression has the value 1, it is just not written anywhere. If you write (x <- 1) or print(x <- 1) you will actually get the number 1 on the console. This suggests three alternative ways of curing your function: 1) add a final line details to the function, making it return the value of 'details' rather than the value of the assignement. 2) Put a set of parenthesis around the assignment, as in (details = summary.lm(m)) 3) Leave the function as it is, but print it explicitly. That is, do not call it as buoyancy(...) but as print(buoyancy(...)) I hope this helps, Ernst Hansen Department of Statistics University of Copenhagen
Your question has been answered by others. I just want to point out that you should not be calling summary.lm() directly, but should call the generic summary() instead. Andy> From: Ajay Shah > > Folks, > > I have a strange situation, which I may have isolated as a bug > report. Or, it could just be that there's something about R that I > don't know. :-) I have attached the data file and the program file but > don't know whether these attachments will make it into the list. Here > is my bugreport.R program -- > > -------------------------------------------------------------- > ------------- > buoyancy <- function(year, taxbase, tax, description, plotname) { > cat("Simple full OLS regression with all data:\n") > logtax = log(tax) > logtaxbase = log(taxbase) > m = lm(logtax ~ logtaxbase) > summary.lm(m) > details = summary.lm(m) > } > > A <- read.table(file="amodi-data.csv", sep=",", col.names=c("year", > "gdp.ag", "gdp.mining", "gdp.manuf", "gdp.elecgas", > "gdp.construction", "gdp.industry", "gdp.services", > "gdp.fc", "indirect.taxes", "subsidies", "j1", > "gdp.mp", "gdp.mp.93", "gdp.deflator", "gdp.fc.93", > "gdp.ag.93", "gdp.industry.93", "gdp.services.93", > "tax.income", "tax.corporation", "tax.direct.others", > "tax.direct", "tax.customs", "tax.excise", > "tax.indirect.others", "tax.indirect", "tax.total")) > A = subset(A, !is.na(A$tax.total)) > buoyancy(A$year, A$gdp.mp, A$tax.income, "Personal income tax > and GDPmp", "p1") > -------------------------------------------------------------- > ------------- > > This program does not work. The summary.lm(m) statement seems to have > no effect. When I run it, I get: > > $ R --slave < bugreport.R > Simple full OLS regression with all data: > > where it is asif the summary.lm(m) statement never occurred. If I put > in a statement print(m) it works, but the summary.lm(m) does not work. > > Now here's what's weird: Suppose I remove the statement that comes > AFTER this summary.lm(m) statement. That is, I don't say > details = summary.lm(m) > as the last line of the function. In this case, the program > works fine! > > I'm most confused. I can't see how putting in an assignment statement > AFTER a function call can contaminate a PREVIOUS statement. I would be > most happy if you could guide me... > > I am running on a nicely-working notebook which runs Debian linux > kernel 2.4.17, and have R 1.8.1 (2003-11-21). I use the `testing' > branch of Debian. > > -ans. > > -- > Ajay Shah Consultant > ajayshah at mayin.org Department of Economic Affairs > http://www.mayin.org/ajayshah Ministry of Finance, New Delhi >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}