[R] Regression

Duncan Murdoch murdoch at stats.uwo.ca
Thu Apr 1 21:05:01 CEST 2010


On 01/04/2010 2:59 PM, Bruce Kindseth wrote:
> I am trying to learn R, and am having problems with doing a simple linear
> regression.  I loaded data from a fixed width file, using wd=c(...), and
> read.fwf(...) and I can read in the file ok and it comes in as vectors in
> columns, which is what I expected.  The problem is when I try to do a linear
> regression, lm=(y~x), I get the following error message, "Error in
> model.frame.default(formula = y ~ x, drop.unused.levels = TRUE) : 
>
>   invalid type (list) for variable 'y'
>
>   

You don't give sample code, so let's assume that you read both x and y as

mydata <- read.fwf( ... )

Then the regression call would be

lm(y ~ x, data=mydata)

If you don't specify the "data=" argument, it will look for *vectors* x 
and y in your workspace to use in the formula.  (It would also accept 
matrices, but not dataframes,
and it sounds as though that's what you gave it.  But you almost 
certainly don't want it to do what it would do with matrices.)

Duncan Murdoch
>  
>
> I tried various things, such as wd=numeric(c(..)), unlist(y) and putting x
> and y in a data frame and attaching it, but nothing helps.  I have searched
> through 3 online manuals, but can't seem to find an answer.  Maybe this is
> so simple that nobody felt the need to address it.  
>
>  
>
> Thanks for your help.
>
>  
>
> B.Kindseth
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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