Regression

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

Regression

Bruce Kindseth
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'

 

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]]

______________________________________________
[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: Regression

Duncan Murdoch
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]]
>
> ______________________________________________
> [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.
>

______________________________________________
[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: Regression

Erik Iverson-3
In reply to this post by Bruce Kindseth
Bruce,

You don't tell us what class of data your y is.  Assuming y is defined
in your enviroment, what does

class(y)

and

str(y)


tell you?  You'll most likely have to fine-tune your data reading
process, or do some post-processing to make sure the y object (and x for
that matter) are the classes you want.

And as always,
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

You can give us samples of your data with the dput function, see ?dput.

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

______________________________________________
[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.