[R] How to write a regression model for finding the radius of a cylinder given height and volume

William Dunlap wdunlap at tibco.com
Fri Feb 14 21:55:54 CET 2014


> URL <- "http://r.789695.n4.nabble.com/file/n4685033/cylinder.dat"
> cyl <- read.table(URL, header = TRUE)
> plot(vol ~ ht, data = cyl)

Since importing data is one of the more error-prone operations in any
computer system, I like to recommend that people always take a look
at what they read before doing much with it.  Assuming that read.table
made a data.frame with numeric columns named 'vol' and 'ht' is often
overly optimistic.  In this case it worked as hoped, but using str() and
summary() on the output of read.table can alert you to problems.
   > str(cyl)
   'data.frame':   14 obs. of  2 variables:
    $ vol: int  7 12 22 27 32 41 48 60 77 85 ...
    $ ht : num  0.5 0.6 1 1.4 1.6 2 2.4 2.8 3.5 4 ...
   > summary(cyl)
         vol               ht       
    Min.   :  7.00   Min.   :0.500  
    1st Qu.: 28.25   1st Qu.:1.450  
    Median : 54.00   Median :2.600  
    Mean   : 61.71   Mean   :2.893  
    3rd Qu.: 96.25   3rd Qu.:4.375  
    Max.   :130.00   Max.   :6.000   
You want to at least check the names, types, and distribution of values
of all the columns.  E.g., if your file contained a "." somewhere in the
second column to indicate a missing value you would see that 'ht' is
not a numeric column:
   > str(badCyl)
   'data.frame':   14 obs. of  2 variables:
    $ vol: int  7 12 22 27 32 41 48 60 77 85 ...
    $ ht : Factor w/ 14 levels ".","0.5","0.6",..: 2 3 4 5 6 7 8 9 10 11 ...
   > summary(badCyl)
         vol               ht    
    Min.   :  7.00   .      :1  
    1st Qu.: 28.25   0.5    :1  
    Median : 54.00   0.6    :1  
    Mean   : 61.71   1      :1  
    3rd Qu.: 96.25   1.4    :1  
    Max.   :130.00   1.6    :1  
                  (Other):8  
plot(vol~ht, data=badCyl) would give you a mysterious looking plot.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Rui Barradas
> Sent: Tuesday, February 11, 2014 3:02 AM
> To: nivek; r-help at r-project.org
> Subject: Re: [R] How to write a regression model for finding the radius of a cylinder given
> height and volume
> 
> Hello,
> 
> It really looked like homework. As for your question, maybe this helps.
> 
> URL <- "http://r.789695.n4.nabble.com/file/n4685033/cylinder.dat"
> cyl <- read.table(URL, header = TRUE)
> plot(vol ~ ht, data = cyl)
> 
> The vectors you want are simply
> 
> cyl$vol
> cyl[["vol"]]
> cyl[[1]]
> 
> and the same for height.
> To find the radius you can note that your formula is equivalent to
> V=pi*r^2*H and use something like (note that the model has no intercept)
> 
> fit <- lm(vol ~ 0 + ht, data = cyl)
> summary(fit)
> 
> r <- sqrt(coef(fit)/pi)
> r
> 
> For linear models in R start by reading the file R-intro.pdf that comes
> with any installation of R, folder 'doc' chapter 11 Statistical models in R.
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 11-02-2014 06:06, nivek escreveu:
> > Thank you for your help. This is not homework however and my difficulty is in
> > the fact that i cannot figure out how the declare a variable for the height
> > or the volume given a data set where the matrix is already formed. I have
> > looked on many sites for the is answer and it seems like it would be an easy
> > one. So for this problem the provided data column 2 is the height data and
> > column 1 is the volume. What command do I use to read those values and form
> > a vector. Thank you for your time.
> >
> >
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/How-to-write-a-
> regression-model-for-finding-the-radius-of-a-cylinder-given-height-and-volume-
> tp4685033p4685090.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > R-help at 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.
> >
> 
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list