simple linear plots with a loop

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

simple linear plots with a loop

Kristi Glover
Hi R users,
I was trying to plot with  a fixed y with many independet variables. I tried this loop but it did not work. any suggestions? I wanted to make 9 plots. This is a just an example data.

dat1<- as.data.frame(matrix(sample(1:20,100,replace=TRUE),ncol=10))
lapply(seq_len(ncol(dat1)),function(i)
{
par(mfrow=c(3,3)),
plot(dat1[,1],dat1[,i+1],
z[,i]<-lm(dat1[,1]~dat1[,i+1]),
abline(z),
summary(z[,i])
}
 here first column is dependent variable and other V2 to V10s are independent variables. Also wanted look the summary (linear model) with each variable.
Thanks for your suggestions
KG
===
 
     
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: simple linear plots with a loop

David Winsemius

On Apr 6, 2014, at 6:07 PM, Kristi Glover wrote:

> Hi R users,
> I was trying to plot with  a fixed y with many independet variables.

It looks like you are plotting with a fixed x.

> I tried this loop but it did not work. any suggestions?

Yes. Explain what "did not work" means. Error? Then post the error  
message. Different results than expected? Then describe.


> I wanted to make 9 plots. This is a just an example data.
>
> dat1<- as.data.frame(matrix(sample(1:20,100,replace=TRUE),ncol=10))
> lapply(seq_len(ncol(dat1)),function(i)
> {
> par(mfrow=c(3,3)),
> plot(dat1[,1],dat1[,i+1],
> z[,i]<-lm(dat1[,1]~dat1[,i+1]),

What were you expecting to happen to "dat1[,i+1]" when "i" was at its  
max?

> abline(z),
> summary(z[,i])
> }
> here first column is dependent variable and other V2 to V10s are  
> independent variables. Also wanted look the summary (linear model)  
> with each variable.
> Thanks for your suggestions
> KG
> ===

Please post in plain text.

>  
> [[alternative HTML version deleted]]

--

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: simple linear plots with a loop

Jim Lemon
In reply to this post by Kristi Glover
On 04/07/2014 11:07 AM, Kristi Glover wrote:

> Hi R users,
> I was trying to plot with  a fixed y with many independet variables. I tried this loop but it did not work. any suggestions? I wanted to make 9 plots. This is a just an example data.
>
> dat1<- as.data.frame(matrix(sample(1:20,100,replace=TRUE),ncol=10))
> lapply(seq_len(ncol(dat1)),function(i)
> {
> par(mfrow=c(3,3)),
> plot(dat1[,1],dat1[,i+1],
> z[,i]<-lm(dat1[,1]~dat1[,i+1]),
> abline(z),
> summary(z[,i])
> }
>   here first column is dependent variable and other V2 to V10s are independent variables. Also wanted look the summary (linear model) with each variable.
> Thanks for your suggestions
> KG

Hi Kristi,
You can get your plots like this:

for(i in 2:10) {
  plot(dat1[,1],dat1[,i],ylab=paste("dat1[,",i,"]",sep=""))
  z<-lm(dat1[,1]~dat1[,i])
  abline(z)
  print(summary(z))
}

This prints the summaries on the console. If you want to get a listing
with plots and summaries together, there are a number of ways, one of
which is the htmlize function in the prettyR package. Save the following
to a file named kg.R:

#title~Example listing with htmlize
z[[1]]<-NULL
png("kg.png",width=600,height=600)
par(mfrow=c(3,3))
for(i in 2:10) {
  plot(dat1[,1],dat1[,i],ylab=paste("dat1[,",i,"]",sep=""))
  z[[i]]<-lm(dat1[,1]~dat1[,i])
  abline(z[[i]])
}
dev.off()
for(i in 1:9) print(summary(z[[i]]))

then:

library(prettyR)
htmlize("kg.R")

Jim

______________________________________________
[hidden email] 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.