Truncated warning or error massages - r

When I try to build a model using PKSFC, I get this message:
[... truncated]
R lists three mistakes and a half (the equations end with *_EC).
I guess I can correct the mistake, but I would like to correct them all in one pass.
I guess there are many more (maybe 15?).
The full message:
5: In sfc.check(vn_mod, fill = FALSE) : Equations
57192.4187750356 + POPAGE_1 * (0.5 * (0 - 55335.9694996733)/POPAGE_1 + 0.2 * (POPAGE - POPAGE_1)/POPAGE_1 - 0.2 * (57192.4187750356 - 0.5 * 55335.9694996733 - 0.508679781654891 * POPAGE_1)/POPAGE_1 + ACPOP_EC)
28.7269160437129 * exp(0.548725 * log(0/1.489181) + 0.704327 * log(0/37.4835480991331) - 0.247704 *
(log(28.7269160437129/37.4835480991331 * (1 + R_SCP_1)) - 0.5 *
log(1.489181) - (1 - 0.5) * log(1.52438302758911)) - 0.5 * 0 -
0.0511046682618056 + WP_EC)
1.25809781599844 * exp(0.2 * log(0/1.46079630578026) + 0.8 * log(PPX * 0/(PPX_1 * 1.1362002193817)) - 0.2 * (log(1.25809781599844) - 0.2 * log(1.46079630578026) - (1 - 0.2) * log(PPX_1 * 1.1362002193817)) -
0.00325177611673638 * (T - 2016) * (T <= 2016) - 0.0576138445722962 + PM_EC)
1.30468806319261 * exp(0.7 * log(0/1.46079630578026) + 0.3 * log(PPX * 0/(PPX_1 * 1.1362002193817)) - 0.2 * (log(1.30468806319261) - 0.7 * log(1.46079630578026) - (1 - 0.7) * log(PPX_1 * 1.1362002193817)) -
0.0033333860060119 * (T - 201 [... truncated]

Related

Compute sequence of summed fractions in R

I want to compute the following sequence using R, without loops, i.e. for cycles.
1 + (2/3) + ((2/3)*(4/5)) + ((2/3)*(4/5)*(6/7)) + ... + ((2/3)*(4/5)*...(20/21))
So far, I tried different approaches with a sequence as well as a while function, but could not came up with a suitable solution. Help would be highly appreciated.
We may use cumprod
v1 <- seq(2, 20, by = 2)
v2 <- seq(3, 21, by = 2)
1 + sum(cumprod(v1/v2))
[1] 4.945724
-manual calculation
1 + (2/3) + ((2/3)*(4/5)) + ((2/3)*(4/5)*(6/7)) + ((2/3)*(4/5)*(6/7) * (8/9)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17) * (18/19)) + ((2/3)*(4/5)*(6/7) * (8/9) * (10/11) * (12/13) * (14/15) * (16/17) * (18/19) * (20/21))
[1] 4.945724

R: How to plot use function which find a root

I construct a user-function:
find.c <- function(q) { f <- function(c) {
(log(7.2 + 6 * c * q - 6) * q^6 * (1-q)^(6-6) * factorial(6) / factorial(6) / factorial(6-6)
+ log(7.2 + 6 * c * q - 5) * q^5 * (1-q)^(6-5) * factorial(6) / factorial(5) / factorial(6-5)
+ log(7.2 + 6 * c * q - 4) * q^4 * (1-q)^(6-4) * factorial(6) / factorial(4) / factorial(6-4)
+ log(7.2 + 6 * c * q - 3) * q^3 * (1-q)^(6-3) * factorial(6) / factorial(3) / factorial(6-3)
+ log(7.2 + 6 * c * q - 2) * q^2 * (1-q)^(6-2) * factorial(6) / factorial(2) / factorial(6-2)
+ log(7.2 + 6 * c * q - 1) * q^1 * (1-q)^(6-1) * factorial(6) / factorial(1) / factorial(6-1)
+ log(7.2 + 6 * c * q - 0) * q^0 * (1-q)^(6-0) * factorial(6) / factorial(0) / factorial(6-0)
- log(7.2)
)}
g <- uniroot(f, lower=0, upper=100, extendInt = "yes")[1]
g}
And I tried to plot:
plot(x = seq(0, 1 , 0.001), find.c(x))
then it gives me: "Error in uniroot(f, lower = 0, upper = 100, extendInt = "yes") :
f() values at end points not of opposite sign"
Also is ther any way I can use 'for' loop to simplify this function? I tried may way with for but function inside function makes very complicated.
I agree with #r2evans suggestion, but I think you can restructure this to be simpler without the nested function.
First, define f with both c= and q= as arguments:
f <- function(c,q) {
(log(7.2 + 6 * c * q - 6) * q^6 * (1-q)^(6-6) * factorial(6) / factorial(6) / factorial(6-6)
+ log(7.2 + 6 * c * q - 5) * q^5 * (1-q)^(6-5) * factorial(6) / factorial(5) / factorial(6-5)
+ log(7.2 + 6 * c * q - 4) * q^4 * (1-q)^(6-4) * factorial(6) / factorial(4) / factorial(6-4)
+ log(7.2 + 6 * c * q - 3) * q^3 * (1-q)^(6-3) * factorial(6) / factorial(3) / factorial(6-3)
+ log(7.2 + 6 * c * q - 2) * q^2 * (1-q)^(6-2) * factorial(6) / factorial(2) / factorial(6-2)
+ log(7.2 + 6 * c * q - 1) * q^1 * (1-q)^(6-1) * factorial(6) / factorial(1) / factorial(6-1)
+ log(7.2 + 6 * c * q - 0) * q^0 * (1-q)^(6-0) * factorial(6) / factorial(0) / factorial(6-0)
- log(7.2)
)}
Then loop over each of the q= values:
mapply(function(...) uniroot(...)[[1]],
list(f), q=seq(0,1,0.001), lower=0, upper=100, extendInt="yes")
# [1] 0.000000 1.076564 1.076460 1.076375 1.076291 1.076206 1.076122
# [8] 1.076037 1.075953 1.075868 1.075784 1.075699 1.075614 1.075530
# etc

Math behind normalization of features (fpfh)?

I am trying to get behind the math of how the features are normalized in fpfh.hpp.
nr_bins_f1 * ((pfh_tuple[0] + M_PI) * d_pi_))
nr_bins_f2 * ((pfh_tuple[1] + 1.0) * 0.5))
nr_bins_f3 * ((pfh_tuple[2] + 1.0) * 0.5))
Why is (pfh_tuple[]+M_PI) * d_pi_ or ...+1.0)*0.5?

I need this diamond shape pattern in R

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
This is my code so far, but it throws an error:
Error in " " * (rows - i - 1) : non-numeric argument to binary operator
Calls: pyramid -> print
Execution halted
#R version 3
pyramid<-function(rows){for (i in rows){print(" "*(rows-i-1)+"*"*(i+1))}
for(j in (rows-1|0|-1)){print(" "*(rows-j)+"*"*(j))}}
rows<-5
pyramid(rows)
You can find plenty of (pseudo-code) examples on the net. That should've been your first approach towards solving your problem. SO is not a free code writing service, and you'll get a much more positive response if you demonstrate a genuine attempt at solving the problem yourself.
That aside, here is a "crude" R implementation of a code example I found here. The code can and probably should be "R-ified", and I encourage you to spend some time doing so. I promise that you'll learn a lot. For example, it should be possible to replace most (all?) explicit for loops by making use of vectorised functions.
diamond <- function(max) {
# Upper triangle
space <- max - 1
for (i in 0:(max - 1)) {
for (j in 0:space) cat(" ")
for (j in 0:i) cat("* ")
cat("\n")
space <- space - 1
}
# Lower triangle
space = 1;
for (i in (max - 1):1) {
for (j in 0:space) cat(" ")
for (j in 0:(i - 1)) cat("* ")
cat("\n")
space <- space + 1
}
}
diamond(5)
# *
# * *
# * * *
# * * * *
#* * * * *
# * * * *
# * * *
# * *
# *

R erasing objects without command line for this [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have a this problem with my code: when I run all the script I get the error Error: object 'i' not found and Error: object 'scp.L50' not found. The data frame and de counter i are erased without order, and after that, (obviously) R cannot find scp.L50 and add information on it. But, I don't know why, when I run just a part of the code (without the #bla bla bla) it works ok.
A few days ago all code was working fine, but now I have this issue. I've tried to update R (smooth sidewalk to full of ingredients) but still not working. Can someone help me?
Thanks!
Here is the code:
library("minpack.lm", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")
library("gdata", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")
sex<-c("FEMALE","MALE","ALL")
files <- list.files(pattern = "txt")
scp.L50<-data.frame()
for (i in 1:length(files)){
dados<-read.table(files[i],header=TRUE,fill = TRUE)
dados<-na.exclude(dados)
dados<-dados[order(dados[,1]),]
rownames(dados)<-1:nrow(dados)
species<-gsub(".txt", "", files[i]);print(paste("Species: ",gsub("_", " ", species)))
if (ncol(dados)==3){
dadosF<-dados[(which(dados[,3]=="F",arr.ind=TRUE)),]; dadosM<-dados[(which(dados[,3]=="M",arr.ind=TRUE)),]; dados.total<-dados
if (length(dadosF[,1])!=0){
dadosF<-dadosF[order(dadosF[,1]),]
} else {
rm(dadosF)}
if (length(dadosM[,1])!=0){
dadosM<-dadosM[order(dadosM[,1]),]
} else {
rm(dadosM)}}
if(ncol(dados)==3){
for (gender in sex){
try({
if (gender=="FEMALE") {
dados<-dadosF
}},silent=TRUE)
try({
if (gender=="MALE"){
dados<-dadosM
}},silent=TRUE)
try({
if (gender=="ALL"){
dados<-dados.total
}},silent=TRUE)
dados<-dados[,-3]
par(mfrow=c(1,1))
if (dados[1,1]==0) {
dados<-dados[-1,]}
unifitted<-nlsLM(Weight~a*Length^b,
data=dados,
start=list(a=0.1,b=3),
weights=wfct(fitted),
control=nls.lm.control(maxiter=1024))
coeff.uni<-as.data.frame(coef(summary(unifitted))[,1:2])
dados<-cbind(dados,resid(unifitted)/dados[,2],predict(unifitted))
sd.dados<-sd(dados[,3])
rownames(dados)<-1:nrow(dados)
sd.dados<-sd(dados[,3])
for (j in nrow(dados):1){
if (dados[j,3]/sd.dados > 2.5){dados<-dados[-j,]
}else{
if (dados[j,3]/sd.dados < -2.5){dados<-dados[-j,]}
}}
interval.cons(25)
unifitted.aver<-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1])
unifitted.aver.resid<-(dataLC[,2]-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1]))/dataLC$AvWeight
dataLC<-cbind(dataLC,unifitted.aver.resid,unifitted.aver)
rownames(dados)<-1:nrow(dados)
dados.1.3<-dados[1:(round(nrow(dados)/3)),]
dados.3.3<-dados[(nrow(dados)-(round(nrow(dados)/3))):nrow(dados),]
assign("best.loop",.GlobalEnv)
sum.resid<-999999999999999999999999999999
polyfitted.1.3<-nlsLM(Weight ~ a * Length^b,
data=dados.1.3,
start=list(a=0.01,b=3),
control=nls.lm.control(maxiter=1024))
coeff.poly.1.3<-as.vector(coef(polyfitted.1.3))
polyfitted.3.3<-nlsLM(Weight ~ a * Length^b,
data=dados.3.3,
start=list(a=0.01,b=3),
control=nls.lm.control(maxiter=1024))
coeff.poly.3.3<-as.vector(coef(polyfitted.3.3))
scp.matrix<-seq(from=dados[30,1], to=dados[(nrow(dados)-30),1], by=1)
r.squared.test<-2
for (scp.loop in scp.matrix){
for (fit.rate in 1:10){
try({
polyfitted.total<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - scp.loop)))) +
(a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - scp.loop))))),
data=dados,
start=list(a1=coeff.poly.1.3[[1]],b1=coeff.poly.1.3[[2]],a2=coeff.poly.3.3[[1]],b2=coeff.poly.3.3[[2]]),
lower=c(a1=coeff.poly.1.3[[1]]-coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]-coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]-coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]-coeff.poly.3.3[[2]]*0.7),
upper=c(a1=coeff.poly.1.3[[1]]+coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]+coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]+coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]+coeff.poly.3.3[[2]]*0.7),
control=nls.lm.control(maxiter=1024))
coeff.poly<-as.vector(coef(polyfitted.total))
polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))) +
(coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))))))/dataLC$AvWeight
polyfitted.aver<-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))) +
((coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))))
polyfitted.resid<-(dados[,2]-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))) +
(coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))))))/dados[,2]
polyfitted<-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))) +
((coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))))
dados<-dados[,-5:-6]
dataLC<-dataLC[,-5:-6]
dados<<-cbind(dados,polyfitted.resid,polyfitted)
dataLC<<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)
phases<-apply(dados,2,function(x) length(x[x<=scp.loop]));phases<-phases[-2:-6]
polyfitted.phase1.res<-dados[1:phases,5]
polyfitted.phase2.res<-dados[(phases+1):nrow(dados),5]
polyfitted.phase1.lr<<-lm(polyfitted.phase1.res~dados[1:phases,1])
polyfitted.phase2.lr<<-lm(polyfitted.phase2.res~dados[(phases+1):nrow(dados),1])
r.squared.lr<-(summary(polyfitted.phase1.lr)$r.squared)+(summary(polyfitted.phase2.lr)$r.squared)
if(sum(resid(polyfitted.total)^2)<sum.resid){
if(r.squared.test>r.squared.lr){
print(">> Best combination found <<")
best.loop<<-polyfitted.total
sum.resid<-sum(resid(polyfitted.total)^2)
rate<<-fit.rate
scp<<-scp.loop
r.squared.test<-r.squared.lr
coeff.poly<<-as.vector(coef(polyfitted.total))
}}else{coeff.poly.only.res<<-coef(polyfitted.total)}
},silent=FALSE)}}
sd.dados<-sd(dados[,5])
rownames(dados)<-1:nrow(dados)
for (j in nrow(dados):1){
if (dados[j,5]/sd.dados > 1.96){dados<-dados[-j,]
}else{
if (dados[j,5]/sd.dados < -1.96){dados<-dados[-j,]}
}}
rownames(dados)<-1:nrow(dados)
rm(j,sd.dados)
keep(dados,dadosF,dadosM,dados.total,interval.cons,files,sex,gender,sure=T)
if (nrow(dados)<150){next}
cut.point<-round((nrow(dados)*0.025),digits=0)
dados<-dados[-1:-cut.point,]
dados<-dados[(nrow(dados)-cut.point:nrow(dados)),]
dados<-dados[order(dados[,1]),]
rownames(dados)<-1:nrow(dados)
print(paste("Calculating ", gender, " data: ",length(dados[,1])," points"))
interval.cons(25)
dados<-dados[,-3:-6]
unifitted<-nlsLM(Weight~a*Length^b,
data=dados,
start=list(a=0.1,b=3),
weights=wfct(fitted),
control=nls.lm.control(maxiter=1024))
coeff.uni<-as.data.frame(coef(summary(unifitted))[,1:2])
unifitted.aver.resid<-(dataLC[,2]-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1]))/dataLC$AvWeight
unifitted.aver<-(coeff.uni[1,1]*dataLC[,1]^coeff.uni[2,1])
dados<-cbind(dados,resid(unifitted)/dados[,2],predict(unifitted))
dataLC<-cbind(dataLC,unifitted.aver.resid,unifitted.aver)
Linf<-exp(0.044 + 0.9841*log(max(dados[,1])))
if (gender=="FEMALE"){
L50<-exp(0.9469*log(Linf) - 0.1162)
}
if (gender=="MALE"){
L50<-exp(0.8915*log(Linf) - 0.1032)
}
if(gender=="ALL"){
L50<-exp(0.8979*log(Linf) - 0.0782)
}
dados.1.3<-dados[1:(round(nrow(dados)/3)),]
dados.3.3<-dados[(nrow(dados)-(round(nrow(dados)/3))):nrow(dados),]
assign("best.loop",.GlobalEnv)
sum.resid<-999999999999999999999999999999
polyfitted.1.3<-nlsLM(Weight ~ a * Length^b,
data=dados.1.3,
start=list(a=0.01,b=3),
control=nls.lm.control(maxiter=1024))
coeff.poly.1.3<-as.vector(coef(polyfitted.1.3))
polyfitted.3.3<-nlsLM(Weight ~ a * Length^b,
data=dados.3.3,
start=list(a=0.01,b=3),
control=nls.lm.control(maxiter=1024))
coeff.poly.3.3<-as.vector(coef(polyfitted.3.3))
scp.matrix<-seq(from=dados[30,1], to=dados[(nrow(dados)-30),1], by=1)
r.squared.test<-2
for (scp.loop in scp.matrix){
for (fit.rate in 1:10){
try({
polyfitted.total<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - scp.loop)))) +
(a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - scp.loop))))),
data=dados,
start=list(a1=coeff.poly.1.3[[1]],b1=coeff.poly.1.3[[2]],a2=coeff.poly.3.3[[1]],b2=coeff.poly.3.3[[2]]),
lower=c(a1=coeff.poly.1.3[[1]]-coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]-coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]-coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]-coeff.poly.3.3[[2]]*0.7),
upper=c(a1=coeff.poly.1.3[[1]]+coeff.poly.1.3[[1]]*0.7,b1=coeff.poly.1.3[[2]]+coeff.poly.1.3[[2]]*0.7,a2=coeff.poly.3.3[[1]]+coeff.poly.3.3[[1]]*0.7,b2=coeff.poly.3.3[[2]]+coeff.poly.3.3[[2]]*0.7),
control=nls.lm.control(maxiter=1024))
coeff.poly<-as.vector(coef(polyfitted.total))
polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))) +
(coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop)))))))/dataLC$AvWeight
polyfitted.aver<-((coeff.poly[[3]] * dataLC$LengthClass^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))) +
((coeff.poly[[1]] * dataLC$LengthClass^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dataLC$LengthClass - scp.loop))))))
polyfitted.resid<-(dados[,2]-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))) +
(coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop)))))))/dados[,2]
polyfitted<-((coeff.poly[[3]] * dados[,1]^coeff.poly[[4]]) * (1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))) +
((coeff.poly[[1]] * dados[,1]^coeff.poly[[2]]) * (1-(1/(1 + exp(-fit.rate * (dados[,1] - scp.loop))))))
dados<-dados[,-5:-6]
dataLC<-dataLC[,-5:-6]
dados<<-cbind(dados,polyfitted.resid,polyfitted)
dataLC<<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)
phases<-apply(dados,2,function(x) length(x[x<=scp.loop]));phases<-phases[-2:-6]
polyfitted.phase1.res<-dados[1:phases,5]
polyfitted.phase2.res<-dados[(phases+1):nrow(dados),5]
polyfitted.phase1.lr<<-lm(polyfitted.phase1.res~dados[1:phases,1])
polyfitted.phase2.lr<<-lm(polyfitted.phase2.res~dados[(phases+1):nrow(dados),1])
r.squared.lr<-(summary(polyfitted.phase1.lr)$r.squared)+(summary(polyfitted.phase2.lr)$r.squared)
if(sum(resid(polyfitted.total)^2)<sum.resid){
if(r.squared.test>r.squared.lr){
print(">> Best combination found <<")
best.loop<<-polyfitted.total
sum.resid<-sum(resid(polyfitted.total)^2)
rate<<-fit.rate
scp<<-scp.loop
r.squared.test<-r.squared.lr
coeff.poly<<-as.vector(coef(polyfitted.total))
}}else{coeff.poly.only.res<<-coef(polyfitted.total)}
},silent=FALSE)}}
coeff.poly<-as.vector(coef(best.loop))
polyfitted.total.rate.scp<-nlsLM(Weight ~ (a2 * Length^b2) * (1/(1 + exp(-fit.rate * (Length - fit.scp)))) +
(a1 * Length^b1) * (1-(1/(1 + exp(-fit.rate * (Length - fit.scp))))),
data=dados,
start=list(a1=coeff.poly[[1]],b1=coeff.poly[[2]],a2=coeff.poly[[3]],b2=coeff.poly[[4]],fit.rate=rate,fit.scp=scp),
lower=c(a1=coeff.poly[[1]]-coeff.poly[[1]]*0.5,b1=coeff.poly[[2]]-coeff.poly[[2]]*0.5,a2=coeff.poly[[3]]-coeff.poly[[3]]*0.5,b2=coeff.poly[[4]]-coeff.poly[[4]]*0.5,fit.rate=rate-(rate*0.2),fit.scp=(scp-(scp*0.2))),
upper=c(a1=coeff.poly[[1]]+coeff.poly[[1]]*0.5,b1=coeff.poly[[2]]+coeff.poly[[2]]*0.5,a2=coeff.poly[[3]]+coeff.poly[[3]]*0.5,b2=coeff.poly[[4]]+coeff.poly[[4]]*0.5,fit.rate=rate+(rate*0.2),fit.scp=(scp+(scp*0.2))),
weights=wfct(fitted),
control=nls.lm.control(maxiter=1024))
coeff.poly.rate.scp<-as.vector(coef(polyfitted.total.rate.scp))
polyfitted.aver.resid<-(dataLC$AvWeight-((coeff.poly.rate.scp[[3]] * dataLC$LengthClass^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]])))) +
(coeff.poly.rate.scp[[1]] * dataLC$LengthClass^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]])))))))/dataLC$AvWeight
polyfitted.aver<-((coeff.poly.rate.scp[[3]] * dataLC$LengthClass^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]]))))) +
((coeff.poly.rate.scp[[1]] * dataLC$LengthClass^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dataLC$LengthClass - coeff.poly.rate.scp[[6]]))))))
polyfitted.resid<-(dados[,2]-((coeff.poly.rate.scp[[3]] * dados[,1]^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]])))) +
(coeff.poly.rate.scp[[1]] * dados[,1]^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]])))))))/dados[,2]
polyfitted<-((coeff.poly.rate.scp[[3]] * dados[,1]^coeff.poly.rate.scp[[4]]) * (1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]]))))) +
((coeff.poly.rate.scp[[1]] * dados[,1]^coeff.poly.rate.scp[[2]]) * (1-(1/(1 + exp(-coeff.poly.rate.scp[[5]] * (dados[,1] - coeff.poly.rate.scp[[6]]))))))
dados<-dados[,-5:-6]
dataLC<-dataLC[,-5:-6]
dados<-cbind(dados,polyfitted.resid,polyfitted)
dataLC<-cbind(dataLC,polyfitted.aver.resid,polyfitted.aver)
coeff.poly<-as.data.frame(coef(summary(polyfitted.total.rate.scp))[,1:2])
phases<-apply(dataLC,2,function(x) length(x[x<=coeff.poly[6,1]]));phases<-phases[-2:-6]
unifitted.phase1.res<-dataLC[,3]; unifitted.phase1.res<-unifitted.phase1.res[1:phases];
unifitted.phase2.res<-dataLC[,3]; unifitted.phase2.res<-unifitted.phase2.res[(phases+1):nrow(dataLC)]
unifitted.phase1.lr<-lm(unifitted.phase1.res~dataLC[1:phases,1]); unifitted.phase2.lr<-lm(unifitted.phase2.res~dataLC[(phases+1):nrow(dataLC),1])
polyfitted.phase1.res<-dataLC[1:phases,5]
polyfitted.phase2.res<-dataLC[(phases+1):nrow(dataLC),5]
polyfitted.phase1.lr<-lm(polyfitted.phase1.res~dataLC[1:phases,1])
polyfitted.phase2.lr<-lm(polyfitted.phase2.res~dataLC[(phases+1):nrow(dataLC),1])
graph.name<-paste(files[i],gender,sep=" ");graph.name<-gsub(".txt", "", graph.name);graph.name<-gsub("_", " ", graph.name)
a.values<-paste("a = ",round(coeff.uni[1,1],digits=4)," (se: ",round(coeff.uni[1,2],digits=4),")",sep="")
b.values<-paste("b = ",round(coeff.uni[2,1],digits=4)," (se: ",round(coeff.uni[2,2],digits=4),")",sep="")
a1.values<-paste("a = ",round(coeff.poly[1,1],digits=4)," (se: ",round(coeff.poly[3,2],digits=4),")",sep="")
b1.values<-paste("b = ",round(coeff.poly[2,1],digits=4)," (se: ",round(coeff.poly[4,2],digits=4),")",sep="")
a2.values<-paste("a = ",round(coeff.poly[3,1],digits=4)," (se: ",round(coeff.poly[1,2],digits=4),")",sep="")
b2.values<-paste("b = ",round(coeff.poly[4,1],digits=4)," (se: ",round(coeff.poly[2,2],digits=4),")",sep="")
scp.values<-paste("SCP = ",round(coeff.poly[6,1],digits=2)," (se: ",round(coeff.poly[6,2],digits=2),")",sep="")
rate.values<-paste("rate = ",round(coeff.poly[5,1],digits=3)," (se: ",round(coeff.poly[5,2],digits=3),")",sep="")
L50.values<-paste("L = ",round(L50,digits=2),sep="")
par(mfrow=c(2,2))
plot(dados[,1],dados[,3],pch=21,col="gray65",bg="white",xlab="Length (cm)",ylab="Residuals",main="Huxley")
points(dataLC[,1],dataLC[,3],pch=21,col="black",bg="black")
try({
lines(dataLC[1:phases,1],predict(unifitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(unifitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Huxley",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,4],lwd=3)
mtext(a.values,line=-1.5,adj=0.05,cex=.6)
mtext(b.values,line=-2.4,adj=0.05,cex=.6)
plot(dados[,1],dados[,5],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Residuals")
points(dataLC[,1],dataLC[,5],pch=21,col="black",bg="black")
abline(v=coeff.poly[6,1],lty=2:2)
try({
lines(dataLC[1:phases,1],predict(polyfitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(polyfitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
abline(v=L50,lty=3)
plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,6],lwd=3)
abline(v=coeff.poly[6,1],lty=2:2)
abline(v=L50,lty=3)
mtext(a1.values,line=-1.5,adj=0.05,cex=.6)
mtext("1",line=-1.6,adj=0.05,cex=.35)
mtext(b1.values,line=-2.4,adj=0.05,cex=.6)
mtext("1",line=-2.5,adj=0.05,cex=.35)
mtext(a2.values,line=-3.3,adj=0.05,cex=.6)
mtext("2",line=-3.4,adj=0.05,cex=.35)
mtext(b2.values,line=-4.2,adj=0.05,cex=.6)
mtext("2",line=-4.3,adj=0.05,cex=.35)
mtext(scp.values,line=-5.1,adj=0.05,cex=.6)
mtext(rate.values,line=-6,adj=0.05,cex=.6)
mtext(L50.values,line=-6.9,adj=0.04,cex=.6)
mtext("50",line=-7,adj=0.06,cex=.35)
par(mfrow=c(2,2),oma = c(0, 0, 1.5, 0))
mtext(graph.name, outer = TRUE, cex = 1.5)
mypath <- file.path("~/Documents/Doutorado/Dados R/Gráficos",paste(graph.name, ".pdf", sep = ""))
mypath.rdata <- file.path("~/Documents/Doutorado/Dados R/Resultados novos",paste(graph.name, ".Rdata", sep = ""))
save(list=ls(all=TRUE),file=mypath.rdata)
pdf(file=mypath)
par(mfrow=c(2,2))
plot(dados[,1],dados[,3],pch=21,col="gray65",bg="white",xlab="Length (cm)",ylab="Residuals",main="Huxley")
points(dataLC[,1],dataLC[,3],pch=21,col="black",bg="black")
try({
lines(dataLC[1:phases,1],predict(unifitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(unifitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Huxley",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,4],lwd=3)
mtext(a.values,line=-1.5,adj=0.05,cex=.6)
mtext(b.values,line=-2.4,adj=0.05,cex=.6)
plot(dados[,1],dados[,5],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Residuals")
points(dataLC[,1],dataLC[,5],pch=21,col="black",bg="black")
abline(v=coeff.poly[6,1],lty=2:2)
try({
lines(dataLC[1:phases,1],predict(polyfitted.phase1.lr),lwd=3,col="gray35"); lines(dataLC[(phases+1):nrow(dataLC),1],predict(polyfitted.phase2.lr),lwd=3,col="gray35")
},silent=TRUE)
abline(h=0,lty=2:2)
abline(v=L50,lty=3)
plot(dados[,1],dados[,2],pch=21,col="gray65",bg="white", main="Polyphasic",xlab="Length (cm)",ylab="Weight (g)")
lines(dados[,1],dados[,6],lwd=3)
abline(v=coeff.poly[6,1],lty=2:2)
abline(v=L50,lty=3)
mtext(a1.values,line=-1.5,adj=0.05,cex=.6)
mtext("1",line=-1.6,adj=0.05,cex=.35)
mtext(b1.values,line=-2.4,adj=0.05,cex=.6)
mtext("1",line=-2.5,adj=0.05,cex=.35)
mtext(a2.values,line=-3.3,adj=0.05,cex=.6)
mtext("2",line=-3.4,adj=0.05,cex=.35)
mtext(b2.values,line=-4.2,adj=0.05,cex=.6)
mtext("2",line=-4.3,adj=0.05,cex=.35)
mtext(scp.values,line=-5.1,adj=0.05,cex=.6)
mtext(rate.values,line=-6,adj=0.05,cex=.6)
mtext(L50.values,line=-6.9,adj=0.04,cex=.6)
mtext("50",line=-7,adj=0.05,cex=.35)
par(mfrow=c(2,2),oma = c(0, 0, 1.5, 0))
mtext(graph.name, outer = TRUE, cex = 1.5)
dev.off()
scp.L50.paste<-cbind(species,gender,round(coeff.poly[6,1],digits=1),round(L50,digits=1),round(AIC(unifitted),digits=2),round(AIC(polyfitted.total.rate.scp),digits=2))
scp.L50<-rbind(scp.L50,scp.L50.paste)
}}
names(scp.L50)<-c("Species","Gender","SCP","L50","AIC Huxley","AIC Polyphasic")
write.csv(scp.L50,file="Summary.csv")
I went through your code and there is indeed a missing bracket. Assuming that the indentation of your code is correct and that you only want to write the summary one time, this is where I think the problem is. Note that I already added the closing bracket on the line where I put my comment:
for (i in 1:length(files)){ # !! I believe you forgot to close this bracket !!
# Some code here..
species<-gsub(".txt", "", files[i]);print(paste("Species: ",gsub("_", " ", species)))
if (ncol(dados)==3){
# Some more code here..
}
if(ncol(dados)==3){
for (gender in sex){
# Wall of code
scp.L50.paste<-cbind(species,gender,round(coeff.poly[6,1],digits=1),round(L50,digits=1),round(AIC(unifitted),digits=2),round(AIC(polyfitted.total.rate.scp),digits=2))
scp.L50<-rbind(scp.L50,scp.L50.paste)
}}
} # !! I believe your closing bracket should be added here !!
names(scp.L50)<-c("Species","Gender","SCP","L50","AIC Huxley","AIC Polyphasic")
write.csv(scp.L50,file="Summary.csv")
EDIT: In other words, it is most likely the most outer for loop that is not closed properly.
The code has 43 opening brackets and 42 closing brackets, so if you're executing it as a script, you can't expect that to run correctly. I don't know for sure where your missing bracket should be, because it depends on what you want to do, but you should take a closer look and attempt to find it. Take a look at your recent changes, have you forgotten to close a loop or something similar?

Resources