leeznar
2007-Dec-28 03:51 UTC
[R] How to catch data from the different dataframes and lm problem?
Dear all: I am a new R-user and I have 2 questions about it. 1) I have a dataframe. Based on ?formulation? and ?subject?, a dataframe is split into 4 dataframes. The example is as follows. Moreover, I want to calculate ?test? value for these 4 dataframes. My question is that the ?test? values not correct and I do not know where the problem is. 2) There are 12 ?test? (y) values from 1). Then, I want to model the relationship between ?concentration? (X) and ?test? (Y) by fitting the linear regression, such lm(Y~X) and this is my target. I think that if I can catch ?test? (Y) values and ?concentration? (X) values into a dataframe, then I can carry out regression. So, how to catch all ?test? values from different dataframes? Or does anyone have better way to get this target? Example: w<-(c(1,1,1,2,2,2,1,1,1,2,2,2)) y<-(c(1,1,1,1,1,1,2,2,2,2,2,2)) z<-(c(1,2,3,1,2,3,1,2,3,1,2,3)) c<-(c(0.1,10,20,0.3,2.5,6,20,25,60,35,40,45)) df<-data.frame(formulation=y, subject=w, time=z, concentration=c) A.split<-split(df, list(df$formulation, df$subject) ) for (j in 1:length(A.split)){ test <- 0 for(i in 2:length(A.split[[j]][["time"]])){ test[i] <- (A.split[[1]][["time"]][i] - A.split[[1]][["time"]][i-1]) * (A.split[[1]][["concentration"]][i] - A.split[[1]][["concentration"]][i-1])* 0.5 test[i]<-test[i]+test[i-1] } output<-data.frame(A.split[[j]][["subject"]],A.split[[j]][["formulation"]],A.split[[j]][["time"]],A.split[[j]][["concentration"]],test) colnames(output)<-list("subject","formulation","time","concentration","test") show(output) } Best regards, Hsin-Ya Lee _____________________________________________________________________________________ ??????Yahoo!??????2.0? http://tw.mg0.mail.yahoo.com/dc/landing
Richard.Cotton at hsl.gov.uk
2007-Dec-28 08:55 UTC
[R] How to catch data from the different dataframes and lm problem?
> 1) I have a dataframe. Based on ?formulation? and ?subject?, a > dataframe is split into 4 dataframes. The > example is as follows. Moreover, I want > to calculate ?test? value for these 4 dataframes. My question is > that the ?test? values not > correct and I do not know where the problem is.What does the variable "test" represent? It isn't clear from your code what it means. (Currently it looks like a cumulative rate of change of concentration with respect to time; are you really sure you want this?)> 2) There are 12 ?test? (y) values from 1). Then, I want to model > the relationship > between ?concentration? (X) and ?test? (Y) by fitting the linearregression,> such lm(Y~X) and this is my target. I > think that if I can catch ?test? (Y) values and ?concentration? (X) > values into > a dataframe, then I can carry out regression. So, how to catch all > ?test? values from > different dataframes? Or does anyone > have better way to get this target?Once you have all your "test" values, you probably want to perform a regression on all the data, in a single data frame, with "formulation" and "subject" as effects, e.g. lm(test~concentration+formulation+subject, data=df) If the subjects are drawn from a large population, it is better to represent them as random effects in a mixed effects model. library(nlme) lme(test~concentration+formulaion, data=df, random=~1|subject) Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential information intended for the addressee(s) only. If this message was sent to you in error, you must not disseminate, copy or take any action in reliance on it and we request that you notify the sender immediately by return email. Opinions expressed in this message and any attachments are not necessarily those held by the Health and Safety Laboratory or any person connected with the organisation, save those by whom the opinions were expressed. Please note that any messages sent or received by the Health and Safety Laboratory email system may be monitored and stored in an information retrieval system. ------------------------------------------------------------------------ ------------------------------------------------------------------------ This e-mail message has been scanned for Viruses and Content and cleared by NetIQ MailMarshal ------------------------------------------------------------------------
Hsin-Ya Lee
2007-Dec-31 05:22 UTC
[R] How to catch data from the different dataframes and lm problem?
Dear Richie: 1) I have a mistake in the code that ?A.split[[1]][["time"]][i]? should replace with ?A.split[[j]][["time"]][i]?. The ?test? value is ?auc? which is a cumulative rate of change of concentration with respect to time. for (j in 1:length(A.split)){ test <- 0 for(i in 2:length(A.split[[j]][["time"]])){ test[i] <- (A.split[[j]][["time"]][i] - A.split[[j]][["time"]][i-1]) * (A.split[[j]][["concentration"]][i] - A.split[[j]][["concentration"]][i-1])* 0.5 test[i]<-test[i]+test[i-1] } output<-data.frame(A.split[[j]][["subject"]],A.split[[j]][["formulation"]],A.split[[j]][["time"]],A.split[[j]][["concentration"]],test) colnames(output)<-list("subject","formulation","time","concentration","test") show(output) } subject formulation time concentration(X) test(Y) 1 1 1 1 0.1 0.00 2 1 1 2 10.0 4.95 3 1 1 3 20.0 9.95 subject formulation time concentration(X) test(Y) 1 1 2 1 20 0.0 2 1 2 2 25 2.5 3 1 2 3 60 20.0 subject formulation time concentration(X) test(Y) 1 2 1 1 0.3 0.00 2 2 1 2 2.5 1.10 3 2 1 3 6.0 2.85 subject formulation time concentration(X) test(Y) 1 2 2 1 35 0.0 2 2 2 2 40 2.5 3 2 2 3 45 5.0 2) Then, I want to pool all "concentration" (X) and pool all "test" (Y) to perform a regression. For example, I used the ?regression? function of Microsoft Excel 2003 and intercept is -0.01894 and X is 0.185758. I think that if I can catch ?test? (Y) values and ?concentration? (X) values into a dataframe, then I can use ?lm? to fit linear models. So, how to catch all ?test? values from different dataframes? Or what should I do? Best regards, Hsin-Ya Lee -- View this message in context: http://www.nabble.com/How-to-catch-data-from-the-different-dataframes-and-lm-problem--tp14521967p14554462.html Sent from the R help mailing list archive at Nabble.com.
Hsin-Ya Lee
2007-Dec-31 06:49 UTC
[R] How to catch data from the different dataframes and lm problem?
Dear all: I used lm to perform a regression in R and and intercept is -0.01894 and X is 0.185758. This is my target. Y<-(c(0,4.95,9.95,0,2.5,20,0,1.1,2.85,0,2.5,5)) X<-(c(0.1,10,20,20,25,60,0.6,2.5,6,35,40,45)) lm(Y~X) anova(wnlm<-lm( Y~X)) summary(wnlm<-lm( Y~X)) I catch test (Y) values and concentration (X) values by myself. Then, here is my question. How to ?auto-catch? test (Y) values and concentration (X) values from the split dataframes? Because I think that if I can catch ?test? (Y) values and ?concentration? (X) values into a dataframe, then I can use ?lm? to fit linear models. So, how to catch all ?test? values from different dataframes? Or what should I do? Best regards, Hsin-Ya Lee -- View this message in context: http://www.nabble.com/How-to-catch-data-from-the-different-dataframes-and-lm-problem--tp14521967p14554872.html Sent from the R help mailing list archive at Nabble.com.