[R] logistic regression

2012-04-03 Thread Melrose2012
I am trying to plot the logistic regression of a dataset (# of living flies
vs days the flies are alive) and then fit a best-fit line to this data.

Here is my code:
plot(fflies$living~fflies$day,xlab=Number of Days,ylab=Number of Fruit
Flies,main=Number of Living Fruit Flies vs Day,pch=16)
alive - (fflies$living)
dead - (fflies$living[1]-alive)
glm.fit - glm(cbind(alive,dead)~fflies$day,family=binomial)
summary(glm.fit)
lines(sort(fflies$day),fitted(glm.fit) [order (fflies$day)],col =
red,lwd=2)
lines(glm.fit,col = red,lwd=2)

My problem is that, while I am pretty sure that I did the 'glm' command
correctly, when I try to plot this as a best-fit line on my data, it does
not fit (clearly on a different scale somehow).

Can anyone enlighten me about what I am doing wrong?  Should I be scaling
one of these somehow?  I've tried various types of scaling but nothing plots
the line on top of the plot (no matter which I scale).

Thanks so much for whatever help you can give me!

Cheers,
Melissa  

--
View this message in context: 
http://r.789695.n4.nabble.com/logistic-regression-tp4530651p4530651.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] logistic regression

2012-04-03 Thread Melrose2012
No, this is related to my own data.

Yes, 'fflies' the dataset - here I am working with two columns:  # of fruit
flies alive vs # of days these flies are alive.

There is no error, it's just that the best-fit line does not plot nicely on
top of my data (see figure attached).
http://r.789695.n4.nabble.com/file/n4530804/fflies1.jpeg 

I then tried to log the raw data as follows and plot:
log.living - log(fflies$living)
plot(log.living~fflies$day,xlab=Number of Days,ylab=Number of Fruit
Flies,main=Number of Living Fruit Flies vs Day,pch=16)
http://r.789695.n4.nabble.com/file/n4530804/fflies2.jpeg 

Any help you can give me would be greatly appreciated.  I am new to R, so
I'm sorry for my beginner's ignorance in learning this program!  :-)

Cheers,
Melissa




--
View this message in context: 
http://r.789695.n4.nabble.com/logistic-regression-tp4530651p4530804.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


Re: [R] logistic regression

2012-04-03 Thread Melrose2012
Thank you for your reply.  

I do understand that I am working in log space with the default link
function of the binomial being logit.  My problem is, I thought that they
way I had written the code, when I did the lines command, it should plot
the best-fit line (found by 'glm') on top of my graph.  As you can see,
however, this clearly did not work and I am off by several factors of
magnitude.  I am not sure if the problem lies in the way I have done the
'glm' or in the plotting.  

If I understand the function correctly, when I do 'glm' of family=binomial,
I am working in log space (hence the link function default being logit.) 
If this is correct, then doesn't either my data have to be converted to log
space also or the results of my 'glm.fit' be converted to linear space (i.e.
using 'inv.logit' from the boot library)?  This was my thought process
behind plotting the data as log (I tried several other obviously useless
iterations of various scalings.)  It appears that you can not do an
inv.logit on the result from the 'glm' command.

If I try to do this:   inv.logit(glm.fit)  
I get an error saying:  Error in plogis(q, location, scale, lower.tail,
log.p) : Non-numeric argument to mathematical function

I'm pretty sure this is a limitation in my understanding of exactly what R
is doing in this logistic regression and I am having a hard time finding
help online or in the R help files that helps me to decipher this problem.  

Can anyone tell me if I have set up the 'glm' wrong or if the error is in
how I am trying to plot the best-fit line on the data?

I am new to these sorts of statistics and data manipulation, so I
acknowledge my ignorance and appreciate whatever help anyone can give me. 

Thank you again!

Cheers,
Melissa

--
View this message in context: 
http://r.789695.n4.nabble.com/logistic-regression-tp4530651p4530959.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


[R] making multiple lines using qqplot

2012-02-09 Thread Melrose2012
Hi Everyone,

I want to make 3 lines on the same graph (not as subplots, all in the same
window, one on top of each other) and I want them to be quantile-quantile
plots (qqplot).  Essentially, I am looking for the equivalent of Matlab's
hold on command to use with qqplot.  I know I can use 'points' or 'lines',
but these do not give me a qqplot (only appear to work as scatter plots).  I
found the syntax 'par(new=TRUE)' but that only seems to work for two lines,
not for three.

My script currently looks like:

qqplot(nq.n5,tq.n5,col=red,xlab=Normal Distribution Quantiles,ylab=t
Distribution Quantiles,main=Quantile-Quantile Plot of Normal vs
t-Distribution for Various Sample Sizes,pch=20)
par(new=TRUE) 
qqplot(nq.n50,tq.n50,col=blue,xlab=,ylab=,pch=20).
par(new=TRUE) 
qqplot(nq.n500,tq.n500,col=green,xlab=,ylab=,pch=20)
legend(topleft,c(n=5,n=50,n=500),fill=c(red,blue,green))

I realize that this only plots the first and the third qqplot because by
doing par(new=TRUE) again, it gets rid of the middle one.  I don't know how
to get around this and get all 3 lines on the same plot.

Can anyone please help me with this syntax?

Thank you very much for your time and advice!

Cheers,
Melissa 

--
View this message in context: 
http://r.789695.n4.nabble.com/making-multiple-lines-using-qqplot-tp4375273p4375273.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


[R] For Loop Error

2012-01-28 Thread Melrose2012
Hi Again,

I am writing a 'for loop' to create a matrix of randomly sampled colors. 
I've written this loop in matlab and it works fine.  I then tried to do it
in R and apparently there is something wrong with my syntax b/c every time I
run the script, the for loop blows up at a different point in the loop.

As you can see, I ask R to clear my workspace each time, so there shouldn't
be any variables in my workspace that are messing this up.

rm(list=ls())

# Sample Size
size - 53  
# Probability of each color based on company
P.company - c(.14,.14,.16,.13,.20,.24)
# Names of colors
color - c('Br','Y','G','R','O','Bl')
# Make an empty matrix that will be filled in by for loop
table.combos - matrix(nrow = 1, ncol = 6);

# For loop will run through choosing a random bag of 53 MMs 1 times
# and create a table enumerating the number of each color in each of these
1 bags
for(i in 1:1) {
  combos - sample(color,size, replace = TRUE, prob = P.company)
  table.combos[i, ] - table(combos)
  colnames(table.combos)-c(Br,Y,G,R,O,Bl)
}

Every time the loop blows up, I get back this error:

Error in table.combos[i, ] - table(combos) : 
  number of items to replace is not a multiple of replacement length

There is no apparent consistency on which i in the loop I get this error. 
Sometimes i = 10, sometimes i = 685, sometimes i = 1954, sometimes i = 59,
etc.

If anyone can please tell me what I am doing wrong, it would be greatly
appreciated!

Cheers,
Melissa

--
View this message in context: 
http://r.789695.n4.nabble.com/For-Loop-Error-tp4336585p4336585.html
Sent from the R help mailing list archive at Nabble.com.

__
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.


[R] finding rows in a matrix that match a vector

2012-01-27 Thread Melrose2012
Hi,

Please excuse my ignorance, but I am just learning R (this is my very first
day programming in R) and having a really hard time figuring out how to do
the following:

I have a matrix that is 1000 row by 6 columns (named 'table.combos') and a 1
row by 6 column vector (named 'mine').  I want to find every row in
'table.combos' that equals 'mine' and then count this number of times that
this is the case.

In matlab, I would use the 'find' command but I can not seem to figure out
what syntax to use for R.

Can anyone please help?  

Again, I'm assuming this is probably a very easy thing to do, but since I am
new to R, I am having a hard time figuring it out.  I did some research on
previous posts and saw that the 'apply' function appears to do osmething
like this, except I don't know what function I am supposed to input into
'apply' to use this.

Thanks in advance for the help!

Cheers,
Melissa

--
View this message in context: 
http://r.789695.n4.nabble.com/finding-rows-in-a-matrix-that-match-a-vector-tp4335216p4335216.html
Sent from the R help mailing list archive at Nabble.com.

__
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.