Standard Deviation in R [closed] - r

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 3 years ago.
Improve this question
I am trying to calculate the standard deviation from scratch in R (I do know R has a build-in function for that), but I'm not getting the correct results, so I don't know what I'm doing wrong.
My.SD<-function(X) {
M<-(sum(X)/length(X))
my.var<-sum((X-M)^2)
StanDev<-sqrt(my.var/length(X))
print(StanDev)
}

I guess you might need to use (length(X)-1) rather than length(x), i.e.,
My.SD<-function(X) {
M<-(sum(X)/length(X))
my.var<-sum((X-M)^2)
StanDev<-sqrt(my.var/(length(X)-1))
print(StanDev)
}
since here it should be sample standard deviation.

Related

How to create this chart 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 3 years ago.
Improve this question
I have a task a bit complicated for my knowledge in R. I need to reproduce this graphic of the figure in R, I performed several searches and could not find anything. The main thing is to be able to reproduce the graphic (it doesn't have to be identical), subtitles are not so important. Any ideas on how to do it or just using another program? Thanks!!
Check also the facet_share() function of the ggpol package, very handy for population pyramids/comparisons

Conditional apply() 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 4 years ago.
Improve this question
I am struggling to create a variable based on a condition in my data frame.
I think the code is quite self-explanatory.
DT$xp_ratio_y<- apply(DT,1,function(x)
if(DT$driv_y_add_flg==1) {
x=DT$driv_y_experience/DT$driv_y_age
} else {
x=0
}
)
I think you're making it too complicated. Just calculate for all then remove those you don't want:
DT$xp_ratio_y <- DT$driv_y_experience/DT$driv_y_age
DT$xp_ratio_y[DT$driv_y_add_flg !=1 ] <- 0

How can I pass a matrix by reference 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 6 years ago.
Improve this question
I am using a function recursively and the function has to update the matrix. I passed the function as an argument, but it seems matrix can only be passed as the pass by value, because the moment program comes out of the scope of function matrix doesn't hold updated values. Since, I have to do multiple updates in the matrix recursively, I have to pass it as a parameter.
Any help is appreciated.
Thanks in Advance

Residuals against fitted values show pattern [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 6 years ago.
Improve this question
I am going to fit a model using linear and quadratic model.
If the graphs of residuals against fitted values show pattern(not random pattern),what is the cause and how can i fix it?
This should be a comment, but I have insufficient rep for that.
The cause of the pattern is that your model doesn't account for something.
The fix is to find what that is, and include it in your model.

Getting the Mean by a Factor [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
All, I wrote this function but it has flaws:
X=cbind(rep(1:5,4),rep(c(1,2,4,8,16),2),c(3.4,4,45,6,4,2,36,4,34,7,8,0,2,4,5,7,9,12,23,1))
getXbarl<-function(Y,l){
xbar=tapply(Y,l,mean)
return(matrix(xbar[l]))#***
}
#It works for the first row:
getXbarl(X[,3],X[,1])
#but not the second row, because the factors are no longer 1:5 here.
getXbarl(X[,3],X[,2])
Please help me write a fix. The issue is that for xbar[l]***, it no longer corresponds to the index.
I think you need ave
ave(X[,3], X[,2])
As a function
getXbarl2 <- function(Y,l) matrix(ave(Y,l))
identical(getXbarl(X[,3], X[,1]), getXbarl2(X[,3], X[,1]))
#[1] TRUE
getXbarl<-function(Y,l){
xbar=tapply(Y,l,mean)
l2=factor(l,labels=1:length(xbar))
return(matrix(xbar[l2]))
}
X=cbind(rep(1:5,4),rep(c(1,2,4,8,16),2),c(3.4,4,45,6,4,2,36,4,34,7,8,0,2,4,5,7,9,12,23,1))
getXbarl(X[,3],X[,1])
getXbarl(X[,3],X[,2])

Resources