小编Rob*_*ert的帖子

使用dplyr从dataframe中抽取子组行

如果我想从不同的组中随机选择一些样本,我使用plyr包和下面的代码

require(plyr)
sampleGroup<-function(df,size) {
  df[sample(nrow(df),size=size),]
}

iris.sample<-ddply(iris,.(Species),function(df) sampleGroup(df,10))
Run Code Online (Sandbox Code Playgroud)

这里从每个物种中选择10个样品.

我的一些数据帧非常大,我的问题是我可以使用与dplyr包相同的sampleGroup函数吗?或者还有另一种方法在dplyr中做同样的事情吗?

编辑

dplyr软件包的0.2版引入了两个新函数来从表sample_n和sample_frac中选择随机行

r sample dplyr

29
推荐指数
3
解决办法
2万
查看次数

如何将嵌套的视图模型绑定到控件的属性

我使用Microsoft的WPF工具包图表控件来编写自己的图表控件.我在这里写博客.我的图表控件将图表中的yaxs堆叠在一起.你可以在文章中看到这一切都很有效.现在我想创建一个控制图表中数据和轴的视图模型.到目前为止,我可以将轴添加到图表中并在图表中显示它们.但是当我尝试添加lineseries时我遇到了问题,因为它有一个DependentAxis和一个InDependentAxis属性.我不知道如何为它分配正确的xAxis和yAxis控件.
下面你看到LineSeriesViewModel的一部分.它具有嵌套的XAxisViewModel和YAxisViewModel属性.

public class LineSeriesViewModel : ViewModelBase, IChartComponent
{

    XAxisViewModel _xAxis;
    public XAxisViewModel XAxis
    {
        get { return _xAxis; }
        set
        {
            _xAxis = value;
            RaisePropertyChanged(() => XAxis);
        }
    }

    //The YAxis Property look the same
}
Run Code Online (Sandbox Code Playgroud)

视图模型都有自己的datatemplate.xaml代码如下所示:

<UserControl.Resources>
    <DataTemplate x:Key="xAxisTemplate" DataType="{x:Type l:YAxisViewModel}">
        <chart:LinearAxis  x:Name="yAxis"  Orientation="Y" Location="Left" Minimum="0"  Maximum="10" IsHitTestVisible="False" Width="50" />
    </DataTemplate>
    <DataTemplate x:Key="yAxisTemplate" DataType="{x:Type l:XAxisViewModel}">
        <chart:LinearAxis x:Name="xAxis"  Orientation="X" Location="Bottom" Minimum="0"  Maximum="100" IsHitTestVisible="False" Height="50" />
    </DataTemplate>

    <DataTemplate DataType="{x:Type l:LineSeriesViewModel}">
        <!--Binding doesn't work on the Dependent and IndependentAxis! …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf datatemplate wpftoolkit

5
推荐指数
1
解决办法
1104
查看次数

如何在函数内使用 lmer

我正在尝试编写一个函数来收集我在脚本中经常使用的一些调用
我在示例中使用了 lme4 包的 sleepstudy 数据这
是我开始使用的函数(的简化版本):

trimModel1 <- function(frm, df) {
  require(LMERConvenienceFunctions)
  require(lme4)

  lm<-lmer(frm,data=df)
  lm.trimmed = romr.fnc(lm, df)
  df = lm.trimmed$data
  # update initial model on trimmed data
  lm<-lmer(frm,data=df)
#   lm@call$formula<-frm
  mcp.fnc(lm)
  lm
}
Run Code Online (Sandbox Code Playgroud)

当我像下面这样调用这个函数时:

(fm1<-trimModel1(Reaction ~ Days + (Days|Subject),sleepstudy))
Run Code Online (Sandbox Code Playgroud)

输出的前三行如下所示:

Linear mixed model fit by REML 
Formula: frm    
Data: df
Run Code Online (Sandbox Code Playgroud)

如果我在控制台中调用了 trimModel1 函数的命令,则模型摘要的前三行如下所示:

Linear mixed model fit by REML 
Formula: Reaction ~ Days + (Days | Subject) 
   Data: sleepstudy 
Run Code Online (Sandbox Code Playgroud)

这种差异是一个问题,因为使用 lme4 包的多个包都使用公式和数据字段。例如,效果包使用这些字段,当我使用上面的 trimModel1 函数时,如下命令将不起作用:

library(effects)
plot(allEffects(fm1))
Run Code Online (Sandbox Code Playgroud)

我在 stackoverflow …

r lme4

5
推荐指数
1
解决办法
2660
查看次数

如何提取lmer输出的固定效果部分的相关性

当你有一个包含大量因子和相互作用的多层次模型时,固定效应矩阵的相关性大小会变得非常大且不清楚.

我可以使用symbolic.cor=Tprint方法中的参数来更清晰地打印摘要,如下所示:

ratbrain <-
within(read.delim("http://www-personal.umich.edu/~bwest/rat_brain.dat"),
{
treatment <- factor(treatment,
labels = c("Basal", "Carbachol"))
region <- factor(region,
labels = c("BST", "LS", "VDB"))
})

print(mod<-lmer(activate ~ region * treatment + (0 + treatment | animal),ratbrain),symbolic.cor=T)
Run Code Online (Sandbox Code Playgroud)

这为大矩阵绘制了一个更清晰的相关矩阵.尽管这个例子的矩阵并不是那么大.但如果我可以绘制相关的热图,那将是很好的.
如何提取固定效果的相关性,以便制作此热图?

编辑:

这是我在答案中创建的功能.

fixeff.plotcorr<-function(mod,...)
{
  #require(GGally) # contains another correlation plot using ggplot2
  require(lme4)

  fixNames<-names(fixef(mod))

  # Simon O'Hanlon's answer:
  # so <- summary(mod)
  # df<-as.matrix(so@vcov@factors$correlation) for version lme4<1.0
  # df<-as.matrix(so$vcov@factors$correlation)  # lme4 >= 1.0

  df<-as.matrix(cov2cor(vcov(mod))) #Ben Bolker's solution

  rownames(df)<-fixNames
  colnames(df)<-abbreviate(fixNames, minlength = 11)

  colsc=c(rgb(241, …
Run Code Online (Sandbox Code Playgroud)

r lme4

5
推荐指数
3
解决办法
3289
查看次数

为什么我不能将viewmodel属性绑定到自定义控件的依赖项属性

我想在我的wpf应用程序中使用颜色选择器,我在这个codeproject页面上看到了一个漂亮的颜色选择器.控件正常工作,直到我想将控件连接到视图模型.我用这个viewmodel创建了一个小测试程序:

public class ColorViewModel : ViewModelBase
{
    public ColorViewModel()
    {
        LineColor = Brushes.Yellow;
    }

    SolidColorBrush _brushColor;
    public SolidColorBrush LineColor
    {
        get { return _brushColor; }
        set
        {
            _brushColor = value;
            RaisePropertyChanged(() => LineColor);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

测试程序有一个文本框和颜色选择器控件:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Please Select a Color" FontWeight="Bold" Margin="10"
               Foreground="{Binding Path=LineColor, UpdateSourceTrigger=PropertyChanged}"/>
     <vw:ColorPickerControlView x:Name="ForeColorPicker" Margin="10"
               CurrentColor="{Binding Path=LineColor, UpdateSourceTrigger=PropertyChanged }"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

在我的测试应用程序中的主窗口的加载事件中,我将viewmodel设置为datacontext,如下所示:

 DataContext = new ColorViewModel();
Run Code Online (Sandbox Code Playgroud)

问题是我似乎无法将viewmodel的LineColor属性绑定到ColorPickerControlView的CurrentColor属性.ColorPickerControlView的CurrentControl属性似乎没问题.构造函数如下所示:

public ColorPickerControlView()
{
    this.DataContext = this;
    InitializeComponent();
    CommandBindings.Add(new CommandBinding(SelectColorCommand, SelectColorCommandExecute));
}
Run Code Online (Sandbox Code Playgroud)

在UserControl的构造函数中,有一行this.DataContext = this; 我读到绑定依赖项属性是必要的.我将viewmodel设置为datacontext时是否覆盖此行,这就是为什么我无法绑定到CurrentColor属性?有没有解决方法?还是我犯了另一个错误?

c# data-binding wpf dependency-properties mvvm

3
推荐指数
1
解决办法
4867
查看次数