通过官网的一个例子来看Activity的分组管理

本文介绍了一个基于Android Support v4库的应用示例,通过Intent过滤器和自定义类别来展示不同组件的功能,如Fragment、Loader等。该应用通过查询安装的活动,实现了模块化的演示界面。



/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.supportv4;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Support4Demos extends ListActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent intent = getIntent();
        String path = intent.getStringExtra("com.example.android.apis.Path");
        
        if (path == null) {
            path = "";
        }

        setListAdapter(new SimpleAdapter(this, getData(path),
                android.R.layout.simple_list_item_1, new String[] { "title" },
                new int[] { android.R.id.text1 }));
        getListView().setTextFilterEnabled(true);
    }

    protected List<Map<String, Object>> getData(String prefix) {
        List<Map<String, Object>> myData = new ArrayList<Map<String, Object>>();

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory("com.example.android.supportv4.SUPPORT4_SAMPLE_CODE");

        PackageManager pm = getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);

        if (null == list)
            return myData;

        String[] prefixPath;
        String prefixWithSlash = prefix;
        
        if (prefix.equals("")) {
            prefixPath = null;
        } else {
            prefixPath = prefix.split("/");
            prefixWithSlash = prefix + "/";
        }
        
        int len = list.size();
        
        Map<String, Boolean> entries = new HashMap<String, Boolean>();

        for (int i = 0; i < len; i++) {
            ResolveInfo info = list.get(i);
            CharSequence labelSeq = info.loadLabel(pm);
            String label = labelSeq != null
                    ? labelSeq.toString()
                    : info.activityInfo.name;
            
            if (prefixWithSlash.length() == 0 || label.startsWith(prefixWithSlash)) {
                
                String[] labelPath = label.split("/");

                String nextLabel = prefixPath == null ? labelPath[0] : labelPath[prefixPath.length];

                if ((prefixPath != null ? prefixPath.length : 0) == labelPath.length - 1) {
                    addItem(myData, nextLabel, activityIntent(
                            info.activityInfo.applicationInfo.packageName,
                            info.activityInfo.name));
                } else {
                    if (entries.get(nextLabel) == null) {
                        addItem(myData, nextLabel, browseIntent(prefix.equals("") ? nextLabel : prefix + "/" + nextLabel));
                        entries.put(nextLabel, true);
                    }
                }
            }
        }

        Collections.sort(myData, sDisplayNameComparator);
        
        return myData;
    }

    private final static Comparator<Map<String, Object>> sDisplayNameComparator =
        new Comparator<Map<String, Object>>() {
        private final Collator   collator = Collator.getInstance();

        public int compare(Map<String, Object> map1, Map<String, Object> map2) {
            return collator.compare(map1.get("title"), map2.get("title"));
        }
    };

    protected Intent activityIntent(String pkg, String componentName) {
        Intent result = new Intent();
        result.setClassName(pkg, componentName);
        return result;
    }
    
    protected Intent browseIntent(String path) {
        Intent result = new Intent();
        result.setClass(this, Support4Demos.class);
        result.putExtra("com.example.android.apis.Path", path);
        return result;
    }

    protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
        Map<String, Object> temp = new HashMap<String, Object>();
        temp.put("title", name);
        temp.put("intent", intent);
        data.add(temp);
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);

        Intent intent = (Intent) map.get("intent");
        startActivity(intent);
    }
}

这里的关键在于下面这几行代码

        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory("com.example.android.supportv4.SUPPORT4_SAMPLE_CODE");

        PackageManager pm = getPackageManager();
        List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);


是把在清单文件AndroidManifest.xml注册category类别为com.example.android.supportv4.SUPPORT4_SAMPLE_CODE的Activity找出来,当然我们也可以自定义一些自己的类别

本例的清单文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Declare the contents of this Android application.  The namespace
     attribute brings in the Android platform namespace, and the package
     supplies a unique name for the application.  When writing your
     own application, the package name must be changed from "com.example.*"
     to come from a domain that you own or have control over. -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.supportv4">

    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="22" />

    <!-- The smallest screen this app works on is a phone.  The app will
         scale its UI to larger screens but doesn't make good use of them
         so allow the compatibility mode button to be shown (mostly because
         this is just convenient for testing). -->
    <supports-screens android:requiresSmallestWidthDp="320"
            android:compatibleWidthLimitDp="480" />

    <application android:label="@string/activity_sample_code"
            android:icon="@drawable/app_sample_code"
            android:hardwareAccelerated="true"
            android:supportsRtl="true">

        <activity android:name="Support4Demos">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".app.SendResult"
                android:theme="@style/ThemeDialogWhenLarge">
        </activity>
        
        <!-- Fragment Support Samples -->

        <activity android:name=".app.FragmentAlertDialogSupport"
                android:label="@string/fragment_alert_dialog_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentArgumentsSupport"
                android:label="@string/fragment_arguments_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentCustomAnimationSupport"
                android:label="@string/fragment_custom_animation_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentHideShowSupport"
                android:label="@string/fragment_hide_show_support"
                android:windowSoftInputMode="stateUnchanged">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentContextMenuSupport"
                android:label="@string/fragment_context_menu_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentDialogSupport"
                android:label="@string/fragment_dialog_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentDialogOrActivitySupport"
                android:label="@string/fragment_dialog_or_activity_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentLayoutSupport"
                android:label="@string/fragment_layout_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentListArraySupport"
                android:label="@string/fragment_list_array_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentLayoutSupport$DetailsActivity" />

        <activity android:name=".app.FragmentMenuSupport"
                android:label="@string/fragment_menu_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentNestingTabsSupport"
                android:label="@string/fragment_nesting_tabs_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>
        
        <activity android:name=".app.FragmentRetainInstanceSupport"
                android:label="@string/fragment_retain_instance_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentReceiveResultSupport"
                android:label="@string/fragment_receive_result_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentStackSupport"
                android:label="@string/fragment_stack_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentTabs"
                android:label="@string/fragment_tabs">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentTabsPager"
                android:label="@string/fragment_tabs_pager">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentPagerSupport"
                android:label="@string/fragment_pager_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.FragmentStatePagerSupport"
                android:label="@string/fragment_state_pager_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.LoaderCursorSupport"
                android:label="@string/loader_cursor_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.LoaderRetainedSupport"
                android:label="@string/loader_retained_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>
        
        <activity android:name=".app.LoaderCustomSupport"
                android:label="@string/loader_custom_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.LoaderThrottleSupport"
                android:label="@string/loader_throttle_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>
        <provider android:name=".app.LoaderThrottleSupport$SimpleProvider"
                  android:authorities="com.example.android.apis.supportv4.app.LoaderThrottle" />

        <activity android:name=".content.LocalServiceBroadcaster"
                android:label="@string/local_service_broadcaster">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>
        <service android:name=".content.LocalServiceBroadcaster$LocalService"
                android:stopWithTask="true" />

        <activity android:name=".content.SimpleWakefulController"
                  android:label="@string/simple_wakeful_controller">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <receiver android:name=".content.SimpleWakefulReceiver" />
        <service android:name=".content.SimpleWakefulService" />

        <activity android:name=".accessibility.AccessibilityManagerSupportActivity"
                android:label="@string/accessibility_manager_title">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".accessibility.AccessibilityDelegateSupportActivity"
                android:label="@string/accessibility_delegate_title">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.SharingSupport"
                  android:label="@string/sharing_support_title">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".app.SharingReceiverSupport"
                  android:label="@string/sharing_receiver_title">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <data android:mimeType="text/plain" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity android:name=".text.BidiFormatterSupport"
                  android:label="@string/bidiformatter_support_title">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".widget.DrawerLayoutActivity"
                  android:label="@string/drawer_layout_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".widget.SwipeRefreshLayoutActivity"
                  android:label="@string/swipe"
                  android:theme="@style/ThemeHoloLight">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".widget.ContentLoadingProgressBarActivity"
                  android:label="@string/content_loading_progress_bar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".widget.SlidingPaneLayoutActivity"
                  android:label="@string/sliding_pane_layout_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".widget.ExploreByTouchHelperActivity"
                  android:label="@string/explore_by_touch_helper_support">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <provider android:authorities="com.example.supportv4.content.sharingsupportprovider"
                  android:name=".content.SharingSupportProvider" />

        <!-- FileProvider Example -->

        <activity android:name=".content.FileProviderExample"
                android:label="@string/file_provider_example">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".widget.NestedScrollActivity"
                  android:label="@string/nested_scroll">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".graphics.RoundedBitmapDrawableActivity"
            android:label="Graphics/RoundedBitmapDrawable">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

        <activity android:name=".graphics.DrawableCompatActivity"
                  android:label="Graphics/DrawableCompat">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>


        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.android.supportv4.my_files"
            android:grantUriPermissions="true"
            android:exported="false">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/my_paths" />
        </provider>


        <activity android:name=".media.TransportControllerActivity"
                android:label="@string/sample_transport_controller_activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="com.example.android.supportv4.SUPPORT4_SAMPLE_CODE" />
            </intent-filter>
        </activity>

    </application>
</manifest>




“我帮你把你的开题报告和你手里的酶活性数据联系起来了 ✅ 你的研究主题是 **“泥炭藓酸对不同类型泥炭沼泽土壤有机碳分解的影响及其调控机制”**。在研究方案里,你特别强调了三方面: 1. **Sphagnum acid(酚类物质)对SOC分解和酶活性的直接作用** 2. **水文条件(水分干旱/湿润)与Sphagnum acid的交互作用** 3. **Fen → Bog 演替过程中的作用机制** 而你现在得到的 **AP、LAP、NAG、CBH、BG 五种水解酶活性数据**,正好是支撑你研究假设的重要指标(SOC分解潜力的代理变量)。 --- ### 🔑 如何把酶活性数据用于开题报告分析 #### 1. 对应研究目标 * **研究目标 1**(明确Sphagnum acid对SOC分解的影响) → 用不同 **PhenolConc**(0、低、高)下的酶活性比较,做 **单因素+交互作用方差分析**。 * **研究目标 2**(水位变化的交互作用) → 你的数据有 **Moisture(Dry vs Wet)**,可以和PhenolConc做 **交互作用效应图**。 * **研究目标 3**(Fen vs Bog演替作用机制) → 你有 **SiteType(RainEdge、RainCore、MinEdge、MinCore)**,可以归类为 **Bog vs Fen**,再比较酶活性模式。 #### 2. 建议图表类型(支撑开题报告结论用) 1. **箱线图/小提琴图** * 展示不同处理下的酶活性分布 * Facet:按 Enzyme × SiteType 分面 👉 直观体现酚类物质、水分对酶活性的抑制/促进作用 2. **折线图(时间序列)** * x轴:Time(T1–T4),y轴:平均酶活性 * 颜色:PhenolConc * 分面:Enzyme × Moisture 👉 展示酶活性随培养时间的动态变化 3. **交互作用效应图** * y轴:酶活性,x轴:PhenolConc * 颜色:Moisture(Dry/Wet) 👉 突出“水分×酚类”的交互效应 4. **热图(Heatmap)** * 行:SiteType × Moisture × PhenolConc * 列:5种酶 * 值:平均酶活性 👉 直观展示不同处理下的酶活性模式,支持“Bog vs Fen差异”分析 #### 3. 统计分析推荐 * **三因素方差分析(ANOVA)**:SiteType × Moisture × PhenolConc * **重复测量/混合效应模型**:考虑时间点和重复性 ```R lmer(Activity ~ SiteType * Moisture * PhenolConc * Time + (1|SampleID), data=data) ``` * **显著性差异标注**:用 emmeans 包做多重比较(Tukey)。 --- 📌 总结: 你这套酶活性数据,完全可以在开题报告里作为 **前期探索性结果/可行性验证**。 * **AP、LAP、NAG、CBH、BG** 代表分解SOC不同组分的潜力,能直接支撑 “Sphagnum acid影响SOC分解机制”。 * 分组设计(SiteType × Moisture × PhenolConc × Time)正好契合研究目标。 * 你只需要在R里画几类图(箱线图、折线图、热图),再跑ANOVA,就能把数据和开题目标挂钩。 --- 要不要我帮你直接写一份 **R脚本分析流程(含画图+ANOVA+热图)**,你可以直接跑在你现在的长表数据上? ”“👌明白了!你需要一个完整的 **“土壤酶活性数据分析流程”**,包含 **数据检查 → 预处理 → 分析 → 可视化**,而且要考虑到各种可能出现的问题(缺失值、异常值、正态性、方差齐性……)。下面我给你整理一个详细的流程,分步骤执行,**每一步都给出在R里的代码示例**。 --- # 📌 土壤酶活性数据分析完整流程(R语言) ## 0. 安装和加载必要包 ```r install.packages(c("tidyverse","car","lme4","lmerTest","emmeans", "ggpubr","pheatmap","performance")) library(tidyverse) library(car) # 方差齐性 library(lme4) # 混合效应模型 library(lmerTest) # lmer的显著性检验 library(emmeans) # 多重比较 library(ggpubr) # 显著性标注 library(pheatmap) # 热图 library(performance) # 模型诊断 ``` --- ## 1. 数据导入与清理 ```r # 导入数据 data <- read.csv("五种酶的长数据格式.csv", fileEncoding="GB18030") # 删除空列 data <- data %>% select(SampleID, SiteType, Moisture, PhenolConc, Enzyme, Time, Replicate, Activity) # 确保变量类型正确 data <- data %>% mutate( SiteType = factor(SiteType), Moisture = factor(Moisture), PhenolConc = factor(PhenolConc), Enzyme = factor(Enzyme), Time = factor(Time, levels=c("T1","T2","T3","T4")), Replicate = factor(Replicate) ) ``` --- ## 2. 数据检查与预处理 ### 2.1 缺失值检查 ```r sum(is.na(data$Activity)) # 查看有多少缺失值 ``` 👉 少量缺失可以直接删除,多组缺失需要在报告中说明。 --- ### 2.2 异常值检查 ```r # 箱线图可视化 ggplot(data, aes(x=Enzyme, y=Activity)) + geom_boxplot() # Z-score法识别 data %>% group_by(Enzyme) %>% mutate(z = (Activity - mean(Activity))/sd(Activity)) %>% filter(abs(z) > 3) ``` 👉 如果发现极端异常值,要判断是实验误差还是合理生物变异。可以: * 保留但在分析中用稳健方法 * 或 log 转换降低极值影响 --- ### 2.3 正态性检验 ```r # 以AP酶为例 ap_data <- subset(data, Enzyme=="AP") # ANOVA模型残差的Shapiro检验 res <- aov(Activity ~ SiteType * Moisture * PhenolConc, data=ap_data) shapiro.test(residuals(res)) ``` 👉 如果 **p < 0.05**,说明偏离正态,考虑对数转换: ```r data$logActivity <- log(data$Activity + 1) ``` --- ### 2.4 方差齐性检验 ```r leveneTest(Activity ~ SiteType * Moisture * PhenolConc, data=ap_data) ``` 👉 如果 **不满足方差齐性**,可以: * 用 Welch ANOVA:`oneway.test(Activity ~ PhenolConc, data=ap_data, var.equal=FALSE)` * 或用非参数 Kruskal-Wallis 检验 --- ### 2.5 重复值处理 * 在 **统计建模** 阶段保留所有重复(避免丢信息)。 * 在 **展示图(折线图/热图)** 阶段可以用均值 ± 标准误。 ```r data_summary <- data %>% group_by(SiteType, Moisture, PhenolConc, Time, Enzyme) %>% summarise(mean_activity = mean(Activity, na.rm=TRUE), se_activity = sd(Activity, na.rm=TRUE)/sqrt(n())) ``` --- ## 3. 统计分析 ### 3.1 三因素 ANOVA ```r anova_res <- aov(Activity ~ SiteType * Moisture * PhenolConc, data=ap_data) summary(anova_res) ``` 👉 用于检验三因素和交互作用对酶活性的影响。 --- ### 3.2 混合效应模型(推荐) 考虑到 **时间点 + 重复测量**: ```r lmer_res <- lmer(Activity ~ SiteType * Moisture * PhenolConc * Time + (1|SampleID), data=data) summary(lmer_res) # 模型诊断 check_model(lmer_res) # 事后比较 emmeans(lmer_res, pairwise ~ PhenolConc | Moisture) ``` --- ## 4. 可视化 ### 4.1 箱线图(处理间比较) ```r ggplot(data, aes(x=PhenolConc, y=Activity, fill=Moisture)) + geom_boxplot(outlier.shape=NA) + facet_grid(Enzyme ~ SiteType) + theme_bw() ``` ### 4.2 折线图(时间变化) ```r ggplot(data_summary, aes(x=Time, y=mean_activity, color=PhenolConc, group=PhenolConc)) + geom_line(size=1) + geom_errorbar(aes(ymin=mean_activity-se_activity, ymax=mean_activity+se_activity), width=0.2) + facet_wrap(~ Enzyme) + theme_bw() ``` ### 4.3 热图(整体模式) ```r heatmap_data <- data %>% group_by(SiteType, Moisture, PhenolConc, Enzyme) %>% summarise(mean_activity = mean(Activity, na.rm=TRUE)) %>% unite("Group", SiteType, Moisture, PhenolConc, sep="_") %>% pivot_wider(names_from = Enzyme, values_from = mean_activity) %>% column_to_rownames("Group") pheatmap(as.matrix(heatmap_data), scale="row", main="不同处理下五种酶活性模式热图") ``` --- ## 5. 结果解释思路 1. **ANOVA/混合模型** → 说明三因素及其交互作用是否显著 2. **箱线图/折线图** → 支撑结论,展示差异和趋势 3. **热图** → 总览五种酶在不同处理下的活性模式 4. **报告中说明**:若数据不满足正态/方差齐性,要说明是否做了数据转换或用非参数检验 --- ✅ 这样你就有了一套从 **数据检查 → 预处理 → 分析 → 图表** 的完整流程,可以直接在开题答辩或论文前期结果里展示。 --- 要不要我帮你先用你上传的那份 **酶活性CSV** 跑一遍 **数据检查(缺失值+异常值+正态性+方差齐性)**,看看数据目前状态如何? ”以上是GPT给我的回复,但是我被限额了, 我现在需要你帮我继续回答,然后我不想要R代码只是以AP酶为例子,因为我要分析所有的五种酶
最新发布
08-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值