Fit saturation growth-rate model in R [closed] - r

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I have a response variable and an independent variable that visually fit to a saturation growth-rate model. How can I fit such model in R? Thank you!

give the nls function a try, but next time please provide some example data. I use the data from this excellent tutorial of a colleague (https://bscheng.com/2014/05/07/modeling-logistic-growth-data-in-r/):
library("car"); library("ggplot2")
#Here's the data
mass<-c(6.25,10,20,23,26,27.6,29.8,31.6,37.2,41.2,48.7,54,54,63,66,72,72.2,
76,75) #Wilson's mass in pounds
days.since.birth<-c(31,62,93,99,107,113,121,127,148,161,180,214,221,307,
452,482,923, 955,1308) #days since Wilson's birth
data<-data.frame(mass,days.since.birth) #create the data frame
plot(mass~days.since.birth, data=data) #always look at your data first!
wilson<-nls(mass~phi1/(1+exp(-(phi2+phi3*days.since.birth))),
start=list(phi1=100,phi2=-1.096,phi3=.002),data=data,trace=TRUE)
#set parameters
phi1<-coef(wilson)[1]
phi2<-coef(wilson)[2]
phi3<-coef(wilson)[3]
x<-c(min(data$days.since.birth):max(data$days.since.birth)) #construct a range of x values bounded by the data
y<-phi1/(1+exp(-(phi2+phi3*x))) #predicted mass
predict<-data.frame(x,y) #create the prediction data frame#And add a nice plot (I cheated and added the awesome inset jpg in another program)
ggplot(data=data,aes(x=days.since.birth,y=mass))+
geom_point(color='blue',size=5)+theme_bw()+
labs(x='Days Since Birth',y='Mass (lbs)')+
scale_x_continuous(breaks=c(0,250,500,750, 1000,1250))+
scale_y_continuous(breaks=c(0,10,20,30,40,50,60,70,80))+
theme(axis.text=element_text(size=18),axis.title=element_text(size=24))+
geom_line(data=predict,aes(x=x,y=y), size=1)

Related

distribution from percentage with R [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have distribution of parameter (natural gas mixture composition) expressed in percents. How to test such data for distribution parameters (it should be gamma, normal or lognormal distribution) and generate random composition based on that parameters in R?
This might be a better question for CrossValidated, but:
it is not generally a good idea to choose from among a range of possible distributions according to goodness of fit. Instead, you should choose according to the qualitative characteristics of your data, something like this:
Frustratingly, this chart doesn't actually have the best choice for your data (composition, continuous, bounded between 0 and 1 [or 0 and 100]), which is a Beta distribution (although there are technical issues if you have values of exactly 0 or 100 in your sample).
In R:
## some arbitrary data
z <- c(2,8,40,45,56,58,70,89)
## fit (beta values must be in (0,1), not (0,100), so divide by 100)
(m <- MASS::fitdistr(z/100,"beta",start=list(shape1=1,shape2=1)))
## sample 1000 new values
z_new <- 100*rbeta(n=1000,shape1=m$estimate["shape1"],
shape2=m$estimate["shape2"])

How to calculate the distance between the Best Fit Curve and the data points? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Hello Everyone!
I am fairly new to R programming and hence I have a small doubt regarding the distance (or offset) of the data-set points from their Best-fit Curve.
The given figure shows some points and a Best-fit Curve for those points.
As we can see some points are very far away from the Best-fit curve and I want to write a code which will tell me the distance (or offset) of all the points from the curve. Then I want to display all the points that are far away from the curve.
I have the equation of the curve and all the data points. The curve has an exponential equation.
The uploaded image is just a approximation of the real figure. I drew this one just as an example.
If someone can tell me what method or functions shoul be used here then it would be a big help.
Thank You.
In many R situations you will actually fit the data with a function such as lm or loess or a glm for instance and the model summary will save residuals with the result.
If you indeed have your own equation then you simply want to take those values of x from the data points - calculate the equation y-values, then subtract them from the corresponding data y-values.
e.g. a toy example
# decay function
x= 1:50
start= 80
decay=0.95
equation_y=start*(decay^x)
plot(x,equation_y, type="l")
# simulated data points
data_y = equation_y + rnorm(50, sd=3)
points(x,data_y, col="red")
# the differences
equation_y - data_y

Model adequacy checking - normal probability plot in R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How can I create a normal probability plot of residuals in R so that there are normal probability values on y-axis?
Normally you'll make the normal probability plot with qqnorm and qqline.
Example:
fit <- lm(resp ~ dep1 + dep2)
qqnorm(fit$residuals, datax=TRUE)
qqline(fit$residuals, datax=TRUE)
You can get residuals vs. prob. with the plot and pnorm:
plot(fit$residuals, pnorm(fit$residuals))
(with prob. on the y-axis)

weighted logistic regression in R - beginner level [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I am trying to do a logistic regression in R with weights, but I dont really know how it works. When I apply weights, something weird happens and all the values appear at 1 but I dont see why? (also how can I fit a line through the points?)
I try to calculate a correlation coefficient for the observed value to the predicted value. Also I am aiming for a plot with "fra" on the y-axis ranging from 0-1, the temp on the x-axis, the fra values in the plot and a line for the regression (something like this example: http://imgur.com/FWevi36)
Thanks!
What I have so far (made up code):
#Dataframe
temp=c(1,1,2,2,3,4,4,5,5,6,6,7,7,8,8)
fra=c(0.0,0.0,0.0,0.0,0.0,0.0,0.5,0.2,0.2,0.3,0.1,0.3,0.4,0.0,0.5)
bin=c(0,0,0,0,0,0,1,1,1,1,1,1,1,0,1)
test1 <- as.data.frame(cbind(temp,bin,fra))
#Overview
plot(test1$temp, test1$bin)
plot(test1$fra)
boxplot(test1$temp ~ test1$bin, horizontal=TRUE)
#Logistic Regression without weight
glmt1 <- glm(test1$bin~test1$temp, family=binomial)
coefficients(summary(glmt1))
fit1 <- fitted(glmt1)
#plot
plot(test1$temp, fit1, ylim=range(0,1))
#line should go to points..???
lines(test1$bin, glmt1$fitted, type="l", col="red")
#with weighted
glmt2 <- glm(test1$bin~test1$temp, family=binomial, weights=test1$fra)
coefficients(summary(glmt2))
fit2 <- fitted(glmt2)
plot(test1$temp, fit2, ylim=range(0,1))
You are only giving a positive weight to cases where bin == 1. That removes all variation in the response variable (you have fit1$bin in the LHS this time). That means your model always predicts 1 no matter what the value of temp1$temp

Number of possible analysis for lat, long, data for different periods in R [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a dataset here with latitude, longitude and salinity for an area. I have these data for three different cases. First case is for normal flow conditions, second is for high flow and third case is for waterlevelrise.
I want to understand how can we use these data and then make some type of analysis.
My data set is uploaded on https://www.dropbox.com/s/285iuyv6bugm48p/dataanalysisforthreetimes.csv
Some of the things that come up to my mind are:
Find the increase or decrease of salinity for each time or even say a pattern.
Mean salinity under different conditions
The code that I used to start in R is as follows:
mydata <- read.csv("dataanalysisforthreetimes.csv")
head(mydata)
library(reshape2)
data1 <- melt(mydata,"Lat","Long")
Would you suggest if I can fit any linear model to my data? Any suggested techniques are highly appreciated.
I want to use R to do the analysis. Can you suggest any reading as well?
mean salinity for all three conditions:
data1 <- melt(mydata,id=c("Lat","Long"))
aggregate(value ~ variable, mean, data=data1)
# variable value
#1 Highflow 4.039384
#2 Levelrise 32.238867
#3 Normal 21.153334
here is how you get the mean fro your conditions. As for linear models, you are probably best googling linear models with spatial autocorrelation in R to get your started.

Resources