我在R中估计了一个带有poLCA-package的模型,并希望将起始值保存到文件中,这样我就可以随时重新估计完全相同的模型.
这是一个模型的起始值列表:
List of 8
 $ : num [1:2, 1:6] 0.219 0.193 0.16 0.193 0.184 ...
 $ : num [1:2, 1:6] 0.0731 0.2054 0.228 0.144 0.2028 ...
 $ : num [1:2, 1:6] 0.0396 0.0965 0.0286 0.1494 0.1609 ...
 $ : num [1:2, 1:6] 0.20998 0.173634 0.105792 0.000588 0.06236 ...
 $ : num [1:2, 1:6] 0.163 0.19 0.167 0.178 0.246 ...
 $ : num [1:2, 1:6] 0.1602 0.1438 0.1963 0.0848 0.2218 ...
 $ : num [1:2, 1:6] 0.0298 0.3022 0.2179 0.094 …我使用ggpairs生成这个图:

这是它的代码:
#load packages
library("ggplot2")
library("GGally")
library("plyr")
library("dplyr")
library("reshape2")
library("tidyr")
#generate example data
dat <- data.frame(replicate(6, sample(1:5, 100, replace=TRUE)))
dat[,1]<-as.numeric(dat[,1])
dat[,2]<-as.numeric(dat[,2])
dat[,3]<-as.numeric(dat[,3])
dat[,4]<-as.numeric(dat[,4])
dat[,5]<-as.numeric(dat[,5])
dat[,6]<-as.numeric(dat[,6])
#ggpairs-plot
main<-ggpairs(data=dat, 
              lower=list(continuous="smooth", params=c(colour="blue")),
              diag=list(continuous="bar", params=c(colour="blue")), 
              upper=list(continuous="cor",params=c(size = 6)), 
              axisLabels='show',
              title="correlation-matrix",
              columnLabels = c("Item 1", "Item 2", "Item 3","Item 4", "Item 5", "Item 6")) +  theme_bw() +
  theme(legend.position = "none", 
        panel.grid.major = element_blank(), 
        axis.ticks = element_blank(), 
        panel.border = element_rect(linetype = "dashed", colour = "black", fill = NA))
main

这个图是一个例子,我使用以下三个ggplot代码生成它.
我将它用于geom_point图:
#------------------------
#lower / geom_point …当我生成一个新的rmarkdown文件(或打开现有的rmarkdown文件)并尝试运行rmarkdown块时,我收到此错误:"错误:尝试使用零长度变量名称".我有Win10并且昨天重新安装了R和Rstudio.我错过了什么?这个错误来自哪里?
```{r cars}
summary(cars)
```
```{r cars}错误:尝试使用零长度变量名
我生成了调查权重.因为离群值调查权重会导致非常大的差异,所以我会从许多统计书中得到一些暗示:我想截断调查权重的前5%和最低5%.我想用dplyr来做这件事.
#generate data
data<-as.data.frame(cbind(sequence(2000),rnorm(2000,mean=3.16,sd=1.355686))) 
names(data)<-c("id","weight")
#This is how far i got
data2<-data %>% mutate(perc.weight=percent_rank(weight)) %>%
                mutate(perc.weight>0.95 | perc.weight<0.05)
在此之后,我有两个新变量.第一个变量给出权重的百分比排名.如果值超出目标范围,则第二个变量显示.
现在,我想用构成这些百分位数边界的权重值替换95-100百分位数和0-5百分位数内的权重.
我会感谢任何帮助!
我想使用dplyr进行一些数据操作.背景:我有一个调查权重和一堆变量(大多数是喜欢项目).我想在有和没有调查权重的情况下对每个类别的频率和百分比求和.
例如,让我们只使用性别变量的频率.结果应该是这样的:
 gender freq    freq.weighted
    1       292     922.2906
    2       279     964.7551
    9         6      21.7338
我会为许多变量做这件事.所以,我决定将dplyr-code放在一个函数中,所以我只需要更改变量并输入less.
#exampledata
gender<-c("2","2","1","2","2","2","2","2","2","2","2","2","1","1","2","2","2","2","2","2","1","2","2","2","2","2","2","2","2","2")
survey_weight<-c("2.368456","2.642901","2.926698","3.628653","3.247463","3.698195","2.776772","2.972387","2.686365","2.441820","3.494899","3.133106","3.253514","3.138839","3.430597","3.769577","3.367952","2.265350","2.686365","3.189538","3.029999","3.024567","2.972387","2.730978","4.074495","2.921552","3.769577","2.730978","3.247463","3.230097")
test_dataframe<-data.frame(gender,survey_weight)
#function
weighting.function<-function(dataframe,variable){
  test_weighted<- dataframe %>% 
    group_by_(variable) %>% 
    summarise_(interp(freq=count(~weight)),
               interp(freq_weighted=sum(~weight)))
  return(test_weighted)
}
result_dataframe<-weighting.function(test_dataframe,"gender")
#this second step was left out in this example:
#mutate_(perc=interp(~freq/sum(~freq)*100),perc_weighted=interp(~freq_weighted/sum(~freq_weighted)*100))
这导致以下错误消息:
Error in UseMethod("group_by_") : 
  no applicable method for 'group_by_' applied to an object of class "formula" 
我尝试过很多不同的事情.首先,我曾经freq=n()计算过频率,但我总是得到一个错误(我检查过,plyr在dplyr之前加载而不是之后 - 它也没有用.).
有任何想法吗?我读了关于标准评估的小插图.但是,我总是遇到问题,不知道什么是解决方案.
我写了一个函数(weighted.sd),给我一些加权统计(如均值,标准差,标准误差和95%置信区间).我想对因子变量(区域)的每个级别应用此函数,然后使用带有错误栏的ggplot2图中每个区域的加权统计信息(因此95%置信区间.
我也试过tapply和for循环.但我没有做对.此外,我喜欢尽可能多地使用dplyr,因为它易于阅读和理解.
这是我最好的尝试:
#example data 
data<-as.data.frame(cbind(rnorm(1:50),as.factor(rnorm(1:50)),rnorm(1:50)))
colnames(data)<-c("index_var","factor_var","weight_var") 
weighted.sd <- function(x,weight){
  na <- is.na(x) | is.na(weight)
  x <- x[!na]
  weight <- weight[!na]  
  sum.w <- sum(weight)
  sum.w2 <- sum(weight^2)
  mean.w <- sum(x * weight) / sum(weight)
  x.var.w<-    (sum.w / (sum.w^2 - sum.w2)) * sum(weight * (x - mean.w)^2)
  x.sd.w<-sqrt((sum.w / (sum.w^2 - sum.w2)) * sum(weight * (x - mean.w)^2))
  SE<- x.sd.w / sqrt(sum(weight))
  error <- qnorm(0.975)*x.sd.w/sqrt(sum(weight))
  left <- mean.w-error
  right <- mean.w+error  
  return(cbind(mean.w,x.sd.w,SE,error,left,right))
}
test<- data %>% 
  group_by(factor_var) %>% 
  do(as.data.frame(weighted.sd(x=index_var,weight=weight_var)))
test
这会导致错误消息(抱歉,部分内容是德语,但您可以使用代码重现它): …
我正在测试新的DiagrammeR包的功能,以便可视化结构方程模型.
我的目标是,获得这样的情节:

也许我需要找到一个关于如何指定边和节点方向的资源,因为 - 有DiagrammeR- 我现在只能做这样的情节:
 
    
devtools::install_github('rich-iannone/DiagrammeR')
library('DiagrammeR')
#----------
test<-grViz("     
digraph CFA{
# latent variables
node [shape=circle]
latent_a [group='a']; 
latent_b [group='b'];
#regressions
latent_a -> latent_b [label='0.279'];
#measurement model for latent a
subgraph A{
node [shape=box]
ind_1; 
ind_2; 
ind_3; 
latent_a -> ind_1 [label='0.636'];
latent_a -> ind_2 [label='0.825'];
latent_a -> ind_3 [label='0.829'];
}
#measurement model for latent b
subgraph B{
node [shape=box]
ind_4 ; 
ind_5 ;
latent_b -> ind_4 [label='1.027'];
latent_b -> ind_5 [label='0.626'];
}
#residuals
node [shape=circle]
e1 ; …我有一个数据框,其中包含从1到5的不同变量.我想以5变为1的方式重新编码一些变量,反之亦然(x = 6-x).我想定义一个变量列表,这些变量将在我的数据帧中像这样重新编码.
这是我使用的方法lapply.我还没有真正了解它.
  #generate example-dataset
    var1<-sample(1:5,100,rep=TRUE)
    var2<-sample(1:5,100,rep=TRUE)
    var3<-sample(1:5,100,rep=TRUE)
    dat<-as.data.frame(cbind(var1,var2,var3))
    recode.list<-c("var1","var3")  
    recode.function<- function(x){          
    x=6-x
     }
    lapply(recode.list,recode.function,data=dat)