r

R function to find suitable values for fitting constants

こ雲淡風輕ζ 提交于 2021-02-10 05:13:22
问题 library(ggplot2) set.seed(1) dataset <- data.frame(X = rnorm(1000)) dfun <- function(x, a, b) 1/(sqrt(2*pi)*b)*exp(-0.5*((x-a)^2/(2*b^2))) ggplot(dataset, aes(x = X)) + geom_histogram(aes(y = ..density..), binwidth = 0.5)+ stat_function(fun = dfun, args = list(a = , b = )) How can I calculate suitable values of a and b in case like this? 回答1: You can compute values for the arguments a and b with nls . Something like the following. dens <- density(dataset$X, n = nrow(dataset)) df_dens <- data

Error with using the mlogit R function: the two indexes don't define unique observations

我的未来我决定 提交于 2021-02-10 05:12:36
问题 I've got a problem with the mlogit funtion in R. My dataset looks like this: personID caseID altID choice a1 a2 a3 a4 1 1 1 1 3 0 3 1 1 1 2 0 1 3 0 1 1 1 3 0 4 4 4 4 1 2 1 0 2 2 1 3 1 2 2 1 2 3 1 3 etc.... I've tried running the following code, performing the model. setV2 <- mlogit.data(data = setV2, choice = "choice", shape = "long", alt.var = "altID", chid.var = "personID") m <- mlogit(choice ~ a1 + a2 + a3 + a4 | -1, rpar = c(a1 = "n", a2 = "n", a3 = "n", a4 = "n"), correlation = FALSE,

How to subset data by filtering and grouping efficiently in R

僤鯓⒐⒋嵵緔 提交于 2021-02-10 05:09:26
问题 I'm working on a project and am looking for some help to make my code run more efficiently. I've searched for similar problems but can't seem to find anything quite as granular as this one. The solution I've come up with is extremely clunky. I'm confident that there must be a more efficient way to do this with a package like dplyr , data.tables , etc. Problem: I have 3 columns of data, 'ids' , 'x.group' , and 'times' . I need to extract the first 3 unique 'ids' that appear in each 'times'

Changing and assigning new variable names with purrr::map()

牧云@^-^@ 提交于 2021-02-10 05:06:39
问题 I am just starting to get the hang of writing functions and using lapply / purrr::map() to make my code more concise, but clearly have not understood it completely yet. In my current example, I want to rename coefficient names of lm_robust objects and then change the lm_robust object to incorporate the new names. I currently do this: library(dplyr) library(purrr) library(estimatr) df <- tibble(interest = rnorm(1000), maturity = runif(1000, 1, 12), genderfemale = rbernoulli(1000), y = 0.5

Changing and assigning new variable names with purrr::map()

倾然丶 夕夏残阳落幕 提交于 2021-02-10 05:03:10
问题 I am just starting to get the hang of writing functions and using lapply / purrr::map() to make my code more concise, but clearly have not understood it completely yet. In my current example, I want to rename coefficient names of lm_robust objects and then change the lm_robust object to incorporate the new names. I currently do this: library(dplyr) library(purrr) library(estimatr) df <- tibble(interest = rnorm(1000), maturity = runif(1000, 1, 12), genderfemale = rbernoulli(1000), y = 0.5

Changing and assigning new variable names with purrr::map()

杀马特。学长 韩版系。学妹 提交于 2021-02-10 04:59:49
问题 I am just starting to get the hang of writing functions and using lapply / purrr::map() to make my code more concise, but clearly have not understood it completely yet. In my current example, I want to rename coefficient names of lm_robust objects and then change the lm_robust object to incorporate the new names. I currently do this: library(dplyr) library(purrr) library(estimatr) df <- tibble(interest = rnorm(1000), maturity = runif(1000, 1, 12), genderfemale = rbernoulli(1000), y = 0.5

Changing and assigning new variable names with purrr::map()

北城余情 提交于 2021-02-10 04:58:02
问题 I am just starting to get the hang of writing functions and using lapply / purrr::map() to make my code more concise, but clearly have not understood it completely yet. In my current example, I want to rename coefficient names of lm_robust objects and then change the lm_robust object to incorporate the new names. I currently do this: library(dplyr) library(purrr) library(estimatr) df <- tibble(interest = rnorm(1000), maturity = runif(1000, 1, 12), genderfemale = rbernoulli(1000), y = 0.5

return NA via RCpp

a 夏天 提交于 2021-02-10 04:37:08
问题 Newbie RCpp question here: how can I make a NumericVector return NA to R? For example, suppose I have a RCpp code that assigns NA to the first element of a vector. // [[RCpp::export]] NumericVector myFunc(NumericVector x) { NumericVector y=clone(x); y[0]=NA; // <----- what's the right expression here? return y; } 回答1: The canonical way to get the correct NA for a given vector type ( NumericVector , IntegerVector , ...) is to use the static get_na method. Something like: y[0] = NumericVector:

How to simply multiply two columns of a dataframe? [duplicate]

别说谁变了你拦得住时间么 提交于 2021-02-10 04:36:06
问题 This question already has answers here : Efficient multiplication of columns in a data frame (4 answers) Closed 3 years ago . My input is a<-c(1,2,3,4) b<-c(1,2,4,8) df<-data.frame(cbind(a,b)) My output should be a<-c(1,2,3,4) b<-c(1,2,4,8) d<-c(1,4,12,32) df<-data.frame(cbind(a,b,c)) can i simply say df$a * df$b please help. I am getting an issue with duplication. they are getting multiplied in matrix form and there is also issue with different length columns. 回答1: In Base R: df$c <- df$a *

Convert English numbers to Persian numbers for ggplot

守給你的承諾、 提交于 2021-02-10 04:16:14
问题 The bounty expires in 3 days . Answers to this question are eligible for a +50 reputation bounty. markus wants to draw more attention to this question. I am working on a data visualization project using ggplot2 . All numbers obtained in the plot (Includes axis-x and axis-y and numbers inside the plot) are in English format like the below plot: but I want the numbers in all plots to be Persian (e.g., ۲۰۱۵ instead of 2015). I have many plots with different numbers. Can anyone help me to convert