[R] finding roots (Max Like Est)
I have this maximum liklihood estimate problem i need to find the roots of the following: [sum (from i=1 to n) ] ((2(x[i]-parameter)/(1+(x[i]-parameter)^2))=0 given to me is the x vector which has length 100 how would I find the roots using R? I have 2 thoughts.. 1 is using a grid search ... eg. brute force, just choosing a whole bunch of different values for my parameter such as parameter=seq(0,100,.1) and this is what I have so far, john=rep(0,length(x)) for(i in 1:length(x)) { john[i]=((x[i]-0)/(1+(x[i]-0)^2)) } sum(john) then john=rep(0,length(x)) for(i in 1:length(x)) { john[i]=((x[i]-.1)/(1+(x[i]-.1)^2)) } sum(john) then john=rep(0,length(x)) for(i in 1:length(x)) { john[i]=((x[i]-.2)/(1+(x[i]-.2)^2)) } sum(john) something like this ... theta=seq(0,100,.1) john=rep(0,length(x)) for(i in 1:length(x)) { john[i]=((x[i]-theta[j])/(1+(x[i]-theta[j])^2)) } sum(john) but something is wrong with my code because its not working. Anyone have any ideas? (I am very new to R and statistical software in general) The 2nd thought was to use the Newton Raphson Method, but, I dont even know where to start with that. Any thoughts help. Thanks -- View this message in context: http://www.nabble.com/finding-roots-%28Max-Like-Est%29-tf4901659.html#a14040895 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] Help with a Loop
I am having trouble getting a loop to work for the following problem. Any help would be much appreciated. Thanks. I need to find the slope and intercept from the linear regression of Drug Level on Day by Participant. There are a total of 37 Participants. I need to store the Participant, Label, Slope, and Intercept in a new data frame. This data is ordered by Participant number 37 total participants. A sample of the data is given below. Label Participant Day DrugLevel 17 0 1 15 1.84179 121 0 1 5 2.10772 147 0 1 7 3.00658 152 0 1 11 2.91729 250 0 1 10 2.75816 289 0 1 13 3.20468 321 0 1 6 2.43389 362 0 1 12 2.0 433 0 1 9 3.03167 469 0 1 8 2.97613 475 0 1 14 2.86934 70 0 2 13 0.68022 210 0 2 8 1.41767 243 0 2 11 1.28867 246 0 2 9 1.53601 247 0 2 6 1.64863 280 0 2 5 1.19795 310 0 2 12 1.24440 343 0 2 10 1.18929 413 0 2 7 1.57207 41 0 3 7 1.87884 74 0 3 8 1.82477 100 0 3 5 2.09422 133 0 3 6 1.91853 134 0 3 12 0.90422 149 0 3 11 1.38232 172 0 3 10 1.55323 216 0 3 9 1.24088 65 0 4 8 2.49412 69 0 4 5 1.79840 This is my thought process of what the loop needs to do but I cant get the correct loop. X1=Day[Participant=="1"] Y1=DrugLevel[Participant=="1"] Coeffs=function(X1,Y1) { lmfirst=lm(Y1~X1) lmfirst$coefficients } Coeffs(X1,Y1) # output slope and intercept here # do same for the next participant X2=Day[Participant=="2"] Y2=DrugLevel[Participant=="2"] Coeffs=function(X2,Y2) { lmfirst=lm(Y2~X2) lmfirst$coefficients } Coeffs(X2,Y2) # output slope and intercept here # do same for the next participant X3=Day[Participant=="3"] Y3=DrugLevel[Participant=="3"] Coeffs=function(X3,Y3) { lmfirst=lm(Y3~X3) lmfirst$coefficients } Coeffs(X3,Y3) # output slope and intercept here # do same for the next participant # etc for the rest of the participants # any ideas? # thanks -- View this message in context: http://www.nabble.com/Help-with-a-Loop-tf4933354.html#a14120625 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.