I am not sure why you would want it, but here it is: individual <- c(1,1,6,8,8,9,9,9,12,12) day <- c(4,17,12,12,17,3,9,22,13,20) condition <- c(0.72, 0.72, 0.67, 0.73, 0.76, 0.65, 0.68, 0.78, 0.73, 0.71)
test1 <- data.frame(individual, day, condition) test1 library(plyr) result=ddply(test1, "individual", function(x) { model <- lm(condition ~ day, data = x) F <- as.numeric((summary(model))$fstatistic) ss <- sum(is.nan(F)) >0 # any(is.nan(F)) # any(is.na(F)) | P <- ifelse(!ss, 1-pf(F[1], F[2],F[3]) , NA) data.frame(coef(model), r.squared = summary(model)$r.squared, p.value = P) }) result ----------------Contact Details:------------------------------------------------------- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Sun, Apr 29, 2012 at 3:58 PM, Kristi Glover <kristi.glo...@hotmail.com>wrote: > Hi Tal, > Thank you so much for the help. I am afraid that I could not make you > understand of my problem. The code you wrote is for whole data set, but I > wanted to do regression based on "Individual" and put coefficient, r2 and p > value. Individual is group variable. > > Here I again run the script > > > > individual <- c(1,1,6,8,8,9,9,9,12,12) > > day <- c(4,17,12,12,17,3,9,22,13,20) > > condition <- c(0.72, 0.72, 0.67, 0.73, 0.76, 0.65, 0.68, 0.78, 0.73, > 0.71) > > test1 <- data.frame(individual, day, condition) > > test1 > individual day condition > 1 1 4 0.72 > 2 1 17 0.72 > 3 6 12 0.67 > 4 8 12 0.73 > 5 8 17 0.76 > 6 9 3 0.65 > 7 9 9 0.68 > 8 9 22 0.78 > 9 12 13 0.73 > 10 12 20 0.71 > > library(plyr) > > result=ddply(test1, "individual", function(x) { > + model <- lm(condition ~ day, data = x) > + coef(model) > + }) > > result > individual (Intercept) day > 1 1 0.7200000 -4.039638e-18 > 2 6 0.6700000 NA > 3 8 0.6580000 6.000000e-03 > 4 9 0.6242403 6.978799e-03 > 5 12 0.7671429 -2.857143e-03 > > > in the result table: I want to add two columns with the information of r2 > and p value. > I hope you understand my problem. thaks for allyour help. I am learning R. > cheers > kristi > > > ------------------------------ > From: tal.gal...@gmail.com > Date: Sun, 29 Apr 2012 12:13:08 +0300 > Subject: Re: [R] r2 and p value dispaly in table > To: kristi.glo...@hotmail.com > CC: r-help@r-project.org > > > You came close. > > Here is how it might be done: > > individual <- rep(c(1,1,6,8,8,9,9,9,12,12),2) > day <- rep(c(4,17,12,12,17,3,9,22,13,20),2) > condition <- rep(c(0.72, 0.72, 0.67, 0.73, 0.76, 0.65, 0.68, 0.78, 0.73, > 0.71),2) > test <- data.frame(individual, day, condition) > > #ind.id <- unique(test$individual) > #ind.list <- lapply(1:length(ind.id), function(i){ subset(test, > test$individual==ind.id[i])}) > #lms <- lapply(ind.list, lm, formula=condition~day) > > require(plyr) > func1 <- function(...) > { > lm(condition~day, data = test) > } > lms <- lapply(1:10, func1) > lms > > func2 <- function(fit) > { > F <- (summary(fit))$fstatistic > P <- 1-pf(F[1], F[2],F[3]) # notice how we must calculate the p-value > data.frame(r.squared = summary(fit)$r.squared, p.value = P) > } > ldply(lms, func2) > > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: tal.gal...@gmail.com | 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > > ---------------------------------------------------------------------------------------------- > > > > > On Sun, Apr 29, 2012 at 9:04 AM, Kristi Glover > <kristi.glo...@hotmail.com>wrote: > > > Hello R User, > I was trying to display r.squared and p value in table from regression, > but I could not display these parameters in the table (matrix) > > for example > individual <- c(1,1,6,8,8,9,9,9,12,12) > day <- c(4,17,12,12,17,3,9,22,13,20) > condition <- c(0.72, 0.72, 0.67, 0.73, 0.76, 0.65, 0.68, 0.78, 0.73, 0.71) > test <- data.frame(individual, day, condition) > ind.id <- unique(test$individual) > ind.list <- lapply(1:length(ind.id), function(i){ subset(test, > test$individual==ind.id[i])}) > lms <- lapply(ind.list, lm, formula=condition~day) > ldply(lms, function(x) x$coefficients) > here I can display coefficients, here I need to write code for r2.squared > > I tried with following script > > summary(ldply(lms, function(x) )$r.squared, $p value) > but it did not work. > > can any one help me? > > thanks > Kristi > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > > > [[alternative HTML version deleted]] ______________________________________________ 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.